how to separate a list into two lists?

Gelonida N gelonida at gmail.com
Sat Aug 6 22:00:46 CEST 2011


On 08/06/2011 08:13 PM, bud wrote:
> On Sun, 07 Aug 2011 01:07:00 +0800, smith jack wrote:
> 
>> 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]
>>
>> i do not want to use loop, any methods to make this done?
> 
> 
> (x,y) = [ [z[i] for z in L] for i in range(len(L[0]))]
> 
> x
> : ['a1', 'a2', 'an']
> 
> y
> : ['b1', 'b2', 'bn']
> 
> 
Asuming you are not an alias of Jack Smith and assuming you did not see
Jack's thread asking the same question:

x,y = unzip(*L)






More information about the Python-list mailing list