how to separate a list into two lists?

Gary Herron gherron at islandtraining.com
Sat Aug 6 19:15:25 CEST 2011


On 08/06/2011 10:07 AM, 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?

List comprehension:

    L1 = [item[0] for item in L]
    L2 = [item[1] for item in L]

which *is* still a loop.  (You are going to have to write *really* 
arcane code to have no loop.)

Gary Herron


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110806/fe00a0ad/attachment.html>


More information about the Python-list mailing list