how to separate a list into two lists?

S

smith jack

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?
 
B

bud

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 for z in L] for i in range(len(L[0]))]

x
: ['a1', 'a2', 'an']

y
: ['b1', 'b2', 'bn']
 
G

Gelonida N

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 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)
 
S

Steven D'Aprano

Gelonida said:
Asuming you [Bud] are not an alias of Jack Smith and assuming you did
not see Jack's thread asking the same question:

That's a strange thing to say when Bud *answered* Jack's question.

x,y = unzip(*L)

What's unzip? It doesn't exist in any version of Python between 1.5 and 3.3
that I have.
 
C

Chris Angelico

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.

Which can be overcome with collections.OrderedDict. But what's dict(L)
going to do? It's going to loop over L, more than once in fact.

I guess the real question is: Why do you wish to avoid a loop?

ChrisA
 
S

Steven D'Aprano

Chris said:
Which can be overcome with collections.OrderedDict. But what's dict(L)
going to do? It's going to loop over L, more than once in fact.

I guess the real question is: Why do you wish to avoid a loop?

I think what the Original Poster actually meant was he wanted to avoid
*writing out an explicit loop*. That is, he wants a one-liner, so he
doesn't have to think about the details of iterating over the list.

When we write:

a = sum(a_sequence)

aren't we doing the same thing really?
 
P

Paul Rubin

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]

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 ;-).
 
G

Gelonida N

Gelonida said:
Asuming you [Bud] are not an alias of Jack Smith and assuming you did
not see Jack's thread asking the same question:

That's a strange thing to say when Bud *answered* Jack's question.

x,y = unzip(*L)

What's unzip? It doesn't exist in any version of Python between 1.5 and 3.3
that I have.


Arg typo:

I meant of course zip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top