Converting a flat list to a list of tuples

M

metiu uitem

Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

I was thinking of something along the lines of:
for (key,number) in list:
print key, number

but it's not working...

Thank you
 
S

Steven D'Aprano

Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

def split_and_combine(L):
newL = []
for i in range(len(L)//2):
newL.append( [L[2*i], L[2*i+1]] )
return newL

Another possibility is a list comprehension:

L = ['a', 1, 'b', 2, 'c', 3]
[[L, L[i+1]] for i in range(len(L)) if i%2 == 0]

Personally, I think that's just about as complex as a single list
comprehension should get. Otherwise it is too easy to create
unmaintainable code.

It is much easier (and probably faster) if you arrange matters so that you
have two lists at the start:

zip( ['a', 'b', 'c'], [1, 2, 3] )

returns [('a', 1), ('b', 2), ('c', 3)]

If you absolutely need the inner tuples to be lists, use a list
comprehension afterwards:

[list(t) for t in zip(['a', 'b', 'c'], [1, 2, 3])]
 
F

Fredrik Lundh

metiu uitem said:
Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

simplest possible (works in all Python versions):

L = ['a', 1, 'b', 2, 'c', 3]

out = []
for i in range(0, len(L), 2):
out.append(L[i:i+2])

or, on one line (works in all modern versions):

out = [L[i:i+2] for i in range(0, len(L), 2)]

or, from the slightly-silly-department:

out = map(list, zip(L[0::2], L[1::2]))

or, using the standard grouping pydiom:

out = []; item = []
for i in L:
item.append(i)
if len(item) == 2:
out.append(item)
item = []
if item:
out.append(item)

etc.

</F>
 
B

bonono

Duncan said:
metiu said:
Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

brilliant.
 
L

Laurent Rahuel

metiu said:
Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

I was thinking of something along the lines of:
for (key,number) in list:
print key, number

but it's not working...

Thank you

Hi,

newList = zip(aList[::2], aList[1::2])
newList
[('a', 1), ('b', 2), ('c', 3)]

Regards,

Laurent.
 
S

Steven D'Aprano

aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

I'm not sure if I should fall to my knees in admiration of a Cool Hack,
or recoil in horror at a Bogus Kludge :)

The code looks like it should return [('a', 'a'), (1, 1), ('b', 'b'), (2,
2), ('c', 'c'), (3, 3)] but of course it does not: the arguments for zip
are not independent.

I guess it is more of a Neat Trick, with a dash of Gotcha For The Unwary.
 
F

Fredrik Lundh

Duncan said:
That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

yesterday, we got locals()["_[1]"]. and now this ?

is "relying on undefined behaviour" perhaps the new black ?

and people are impressed? it's like my old Z80 days, when
some folks thought it was truly amazing that call(11) printed
the raw contents of the entire memory to the screen...

</F>
 
?

=?ISO-8859-1?Q?Andr=E9?= Malo

* Duncan Booth said:
metiu said:
Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

Though it looks nice, it's an implementation dependant solution. What if
someone changes zip to fetch the second item first?

nd
 
B

bonono

André Malo said:
* Duncan Booth said:
metiu said:
Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

Though it looks nice, it's an implementation dependant solution. What if
someone changes zip to fetch the second item first?
I believe someone should change the behaviour in the next release(is
that 2.4.3 or 2.5?), then it will give us the hard lesson :)

Just saying it is no good is not going to stop people from doing it.
 
G

George Sakkis

Laurent Rahuel said:
Hi,

newList = zip(aList[::2], aList[1::2])
newList
[('a', 1), ('b', 2), ('c', 3)]

Regards,

Laurent

Or if aList can get very large and/or the conversion has to be
performed many times:

from itertools import islice
newList = zip(islice(aList,0,None,2), islice(aList,1,None,2))

George
 
B

Bengt Richter

metiu said:
Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]
Thank you for that. That is cool ;-)

Regards,
Bengt Richter
 
B

Bengt Richter

Duncan said:
That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

yesterday, we got locals()["_[1]"]. and now this ?
I don't really think those are comparable.
is "relying on undefined behaviour" perhaps the new black ?
Is it really undefined? If so, IMO it should be defined to
do what it apparently does.
and people are impressed? it's like my old Z80 days, when
some folks thought it was truly amazing that call(11) printed
the raw contents of the entire memory to the screen...
You really don't think it was cool? Or could be well defined? ;-)

Hm, actually, something tells me I've seen some variation of this before,
but I can't think of the context off hand.

Regards,
Bengt Richter
 
B

Bengt Richter

* Duncan Booth said:
metiu said:
Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

Though it looks nice, it's an implementation dependant solution. What if
someone changes zip to fetch the second item first?
That would be a counter-intuitive thing to do. Most things go left->right
in order as the default assumption.

Regards,
Bengt Richter
 
B

Bengt Richter

Duncan Booth said:
aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

That behavior is currently an accident.
http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
Alan Isaac
That says
"""
ii. The other problem is easier to explain by example.
Let it=iter([1,2,3,4]).
What is the result of zip(*[it]*2)?
The current answer is: [(1,2),(3,4)],
but it is impossible to determine this from the docs,
which would allow [(1,3),(2,4)] instead (or indeed
other possibilities).
"""
IMO left->right is useful enough to warrant making it defined behaviour,
not an accident. Isn't it(),it() well defined for a given iterator?
So is the question whether zip will access its referenced input iterators
in some peculiar order? Is the order of zip(a,b) accesses to a and b
undefined? If, so, IMO it's reasonable to make it defined as if
... return list(tuple([it.next() for it in its])
... for its in [[iter(a) for a in args]]
... for _ in iter(lambda:0,1))
...
>>> aList = ['a', 1, 'b', 2, 'c', 3]
>>> it = iter(aList)
>>> zip(it, it) [('a', 1), ('b', 2), ('c', 3)]
>>> it = iter(aList)
>>> zip(it, it, it) [('a', 1, 'b'), (2, 'c', 3)]
>>> it = iter(aList)
>>> zip(it) [('a',), (1,), ('b',), (2,), ('c',), (3,)]
>>> zip(range(3), range(4)) [(0, 0), (1, 1), (2, 2)]
>>> zip(range(4), range(3))
[ (0, 0), (1, 1), (2, 2)]

(I just hacked this out, so maybe it's not bullet-proof, but the point is,
I think there's no reason not to define the behaviour of zip to cycle
through its arguments in the intuitive way).

Regards,
Bengt Richter
 
D

Dave Hansen

Duncan said:
That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:

aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]
[...]

Hm, actually, something tells me I've seen some variation of this before,
but I can't think of the context off hand.

Earlier this fall I posted a question about iterating over a sequence
while modifying it. Google should bring it up.

This strikes me as the same idea, only inside-out...

Regards,
-=Dave
 
B

Bengt Richter

Laurent Rahuel said:
Hi,

newList = zip(aList[::2], aList[1::2])
newList
[('a', 1), ('b', 2), ('c', 3)]

Regards,

Laurent

Or if aList can get very large and/or the conversion has to be
performed many times:

from itertools import islice
newList = zip(islice(aList,0,None,2), islice(aList,1,None,2))
Or, if you want to include fractional groups at the end
>>> aList = ['a', 1, 'b', 2, 'c', 3]
>>> from itertools import groupby
>>> def grouper(n):
... def git():
... while True:
... for _ in xrange(n): yield 0
... for _ in xrange(n): yield 1
... git = git()
... def grouper(_): return git.next()
... return grouper
...
>>> [tuple(g) for _, g in groupby(aList, grouper(2))] [('a', 1), ('b', 2), ('c', 3)]
>>> [tuple(g) for _, g in groupby(aList, grouper(3))] [('a', 1, 'b'), (2, 'c', 3)]
>>> [tuple(g) for _, g in groupby(aList, grouper(4))]
[('a', 1, 'b', 2), ('c', 3)]

Regards,
Bengt Richter
 
B

bonono

Bengt said:
* Duncan Booth said:
metiu uitem wrote:

Say you have a flat list:
['a', 1, 'b', 2, 'c', 3]

How do you efficiently get
[['a', 1], ['b', 2], ['c', 3]]

That's funny, I thought your subject line said 'list of tuples'. I'll
answer the question in the subject rather than the question in the body:

aList = ['a', 1, 'b', 2, 'c', 3]
it = iter(aList)
zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]

Though it looks nice, it's an implementation dependant solution. What if
someone changes zip to fetch the second item first?
That would be a counter-intuitive thing to do. Most things go left->right
in order as the default assumption.
I have to admit that the poster was right as there is nothing stop the
implementor to do it this way and still gives you the defined result of
zip(). One scenario I can think of is that it can be implemented to run
in parallel, taking "n" results from "n" streams at the same time and
when all n arrived, pump the resultinging tuple out and repeat.

Why it needs to be done this way or is it desirable is another story.

And I doubt the current behaviour will go away, unless they really want
to break some codes to make the point.
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top