[EMAIL PROTECTED] wrote:
James Fassett:
# the first Pythonic attempt using comprehensions
result_list = [x[0] for x in tuple_list]
This has the virtue of working for tuples of any length and doing the
minimal work required.
# the final functional way
[result_list, _] = zip(*tuple_list)
This requires the tuples in tuple_list to be of length 2. It also
produces a second list that is then tossed away.
The list comprehension is quite more readable to me, so I suggest you
to use it. It's probably the default way to do it in Python.
It also has two virtues that the non-equivalent alternative lacks.
If you want functional code this is another way (I have not tested the
relative performance but it may be quick):
tuple_list = (
... ('John', 'Doe'),
... ('Mark', 'Mason'),
... ('Jeff', 'Stevens'),
... ('Bat', 'Man')
... )
from operator import itemgetter
map(itemgetter(0), tuple_list)
['John', 'Mark', 'Jeff', 'Bat']
This again makes just one list from tuples of any length.
Some of the other alternatives in another post do minimal work but only
work with pairs.
tjr
--
http://mail.python.org/mailman/listinfo/python-list