Can this be done with list comprehension?

Karlo Lozovina _karlo_ at _mosor.net_
Sun Jun 8 00:49:14 CEST 2008


Gary Herron <gherron at islandtraining.com> wrote in
news:mailman.196.1212878671.1044.python-list at python.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.
|__|_|__||_____|_____|



More information about the Python-list mailing list