December 5th, 2004, 05:45 PM
simple list manipulation
i have a nested tuple/list such
i want to create out of it another list with the elements of the second column for example : b=[2,2,4] . Do you have any idea ?
i can just print such result but not as a new list
December 5th, 2004, 06:07 PM
List comprehensions to the rescue!
There are other ways of doing it, but this is by far the simplest.
>>> [x for x in a]
[2, 2, 4]
In Python 2.4 you can also use a generator expression to create a generator that will retrieve the results when needed. For a large list this can be much more efficient:
Dave - The Developers' Coach
>>> gen = (x for x in a)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Last edited by DevCoach; December 5th, 2004 at 06:13 PM.