Can this be done with list comprehension?

Duncan Booth duncan.booth at invalid.invalid
Sun Jun 8 11:54:42 CEST 2008


Lie <Lie.1296 at gmail.com> wrote:

> On Jun 8, 7:27 am, "Terry Reedy" <tjre... at udel.edu> wrote:
>> "Karlo Lozovina" <_karlo_ at _mosor.net_> wrote in message
>>
>> news:Xns9AB7858EC056mosornet at 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.



More information about the Python-list mailing list