how to separate a list into two lists?

Paul Rubin no.email at nospam.invalid
Sun Aug 7 07:29:44 CEST 2011


smith jack <thinke365 at gmail.com> writes:
> 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?

That is called unzipping and there is a sneaky idiom for it:

 L1,L2 = zip(*L)

Your homework assignment is figuring out how that works ;-).



More information about the Python-list mailing list