473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can this be done with list comprehension?

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
7 897
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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
7
by: Chris P. | last post by:
Hi. I've made a program that logs onto a telnet server, enters a command, and then creates a list of useful information out of the information that is dumped to the screen as a result of the...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
18
by: a | last post by:
can someone tell me how to use them thanks
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
4
by: bullockbefriending bard | last post by:
Given: class Z(object): various defs, etc. class ZList(list): various defs, etc. i would like to be able to replace
10
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
4
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.