By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Can this be done with list comprehension?

P: n/a
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 ;).
--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Karlo Lozovina wrote:
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).

P.S.
In case you're wondering, it has to be done in one line ;).
Jun 27 '08 #2

P: n/a
Gary Herron <gh*****@islandtraining.comwrote in
news:ma*************************************@pytho n.org:
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?

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jun 27 '08 #3

P: n/a
On Jun 8, 8:31 am, Karlo Lozovina <_karlo_@_mosor.net_wrote:
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]
Jun 27 '08 #4

P: n/a

"Karlo Lozovina" <_karlo_@_mosor.net_wrote in message
news:Xn*********************@161.53.160.64...
| 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

Jun 27 '08 #5

P: n/a
Lie
On Jun 8, 7:27*am, "Terry Reedy" <tjre...@udel.eduwrote:
"Karlo Lozovina" <_karlo_@_mosor.net_wrote in message

news:Xn*********************@161.53.160.64...
| 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.
Jun 27 '08 #6

P: n/a
Lie <Li******@gmail.comwrote:
On Jun 8, 7:27*am, "Terry Reedy" <tjre...@udel.eduwrote:
>"Karlo Lozovina" <_karlo_@_mosor.net_wrote in message

news:Xn*********************@161.53.160.64...
| 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.
and also throws the list away as soon as it creates it. Didn't you read
earlier in this thread: list methods that mutate the list in-place return
None.
Jun 27 '08 #7

P: n/a
Le Sunday 08 June 2008 02:27:54 Terry Reedy, vous avez écrit*:
"Karlo Lozovina" <_karlo_@_mosor.net_wrote in message
news:Xn*********************@161.53.160.64...

| 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

--
http://mail.python.org/mailman/listinfo/python-list
A one liner, though it's a bit lispy :

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

--
_____________

Maric Michaud
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.