Questions about tuple?

Chris Liechti cliechti at gmx.net
Fri Nov 8 19:10:14 EST 2002


Mindy <csshi99 at yahoo.com> wrote in news:mailman.1036801694.17667.python-
list at python.org:

> Hey, given a tuple t(a,b), is there any function in
> Python that I can get a, b individually from t?
> Actually, I have a list whose element is tuples. Say:
> 
> tuple_list = [(1,'a'),(3,'b'),(2,'l'),(8,'p')]
> 
> Then I want to get two lists from this tuple_list. The
> first list contains the first elements in the tuple of
> the tuple_list. 
> 
> first_list = [1,3,2,8]
> 
> And the second list contains all the second elements,
> also maintaining the original order as in tuple_list.
> 
> second_list = ['a','b','l','p']
> 
> So how to get this easily? Thanks!

the magic of zip will do it for you ;-)

>>> first_list, second_list = zip(*tuple_list)

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list