how to separate a list into two lists?

Tim Roberts timr at probo.com
Sat Aug 6 20:58:30 EDT 2011


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

There will always be a loop.  It might not be written with a "for"
statement, but there will always be a loop.

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

I did momentarily consider the following slimy solution:
  L1 = dict(L).keys()
  L2 = dict(L).values()
but that reorders the tuples.  They still correspond, but in a different
order.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list