how to separate a list into two lists?

Emile van Sebille emile at fenx.com
Sat Aug 6 13:24:10 EDT 2011


On 8/6/2011 10:07 AM smith jack said...
> if a list L is composed with tuple consists of two elements, that is
> L = [(a1, b1), (a2, b2) ... (an, bn)]
>
> is there any simple way to divide this list into two separate lists , such that
> L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]
>

 >>> L = [('a1', 'b1'), ('a2', 'b2'),('an', 'bn')]
 >>> zip(*L)
[('a1', 'a2', 'an'), ('b1', 'b2', 'bn')]


Emile





More information about the Python-list mailing list