Can this be done with list comprehension?

K

Karlo Lozovina

This is what I'm trying to do (create a list using list comprehesion, then
insert new element at the beginning of that list):

result = [someFunction(i) for i in some_list].insert(0, 'something')

But instead of expected results, I get None as `result`. If instead of
calling `insert` method I try to index the list like this:

result = [someFunction(i) for i in some_list][0]

It works as expected. Am I doing something wrong, or I can't call list
methods when doing list comprehension?

P.S.
In case you're wondering, it has to be done in one line ;).
 
G

Gary Herron

Karlo said:
This is what I'm trying to do (create a list using list comprehesion, then
insert new element at the beginning of that list):

result = [someFunction(i) for i in some_list].insert(0, 'something')

But instead of expected results, I get None as `result`. If instead of
calling `insert` method I try to index the list like this:

result = [someFunction(i) for i in some_list][0]

It works as expected. Am I doing something wrong, or I can't call list
methods when doing list comprehension?

The problem is that list methods like insert do not return a list --
they modify it in place. If you do
a = [1,2,3]
a.insert(0, 'something')
then a will have the results you expect, but if you do
b = a.insert(0,'something')
you will find b to be None (although a will have the expected list).
 
K

Karlo Lozovina

The problem is that list methods like insert do not return a list --
they modify it in place. If you do
a = [1,2,3]
a.insert(0, 'something')
then a will have the results you expect, but if you do
b = a.insert(0,'something')
you will find b to be None (although a will have the expected list).

I figured that out few minutes ago, such a newbie mistake :). The fix I
came up with is:

result = ['something'] + [someMethod(i) for i in some_list]

Are there any other alternatives to this approach?
 
J

John Machin

This is what I'm trying to do (create a list using list comprehesion, then
insert new element at the beginning of that list):

result = [someFunction(i) for i in some_list].insert(0, 'something')

result = ['something'] + [someFunction(i) for i in some_list]
 
T

Terry Reedy

| I figured that out few minutes ago, such a newbie mistake :). The fix I
| came up with is:
|
| result = ['something'] + [someMethod(i) for i in some_list]
|
| Are there any other alternatives to this approach?

result = [something]
result.extend(someMethod(i) for i in some_list)

avoids creating and deleting an intermediate list
 
L

Lie

| I figured that out few minutes ago, such a newbie mistake :). The fix I
| came up with is:
|
|  result = ['something'] + [someMethod(i) for i in some_list]
|
| Are there any other alternatives to this approach?

result = [something]
result.extend(someMethod(i) for i in some_list)

avoids creating and deleting an intermediate list

or:
result = ['something'].extend(someMethod(i) for i in some_list)

it also avoids intermediate list and is one line.
 
M

Maric Michaud

Le Sunday 08 June 2008 02:27:54 Terry Reedy, vous avez écrit :
| I figured that out few minutes ago, such a newbie mistake :). The fix I
| came up with is:
|
| result = ['something'] + [someMethod(i) for i in some_list]
|
| Are there any other alternatives to this approach?

result = [something]
result.extend(someMethod(i) for i in some_list)

avoids creating and deleting an intermediate list

A one liner, though it's a bit lispy :

list(itertools.chain((something,), (someMethod(i) for i in some_list)))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top