Converting a flat list to a list of tuples

Duncan Booth duncan.booth at invalid.invalid
Wed Nov 23 03:44:49 EST 2005


Bengt Richter wrote:

> On Tue, 22 Nov 2005 13:26:45 +0100, "Fredrik Lundh"
> <fredrik at pythonware.com> wrote: 
>>Duncan Booth wrote:
>>
>>> >>> it = iter(aList)
>>> >>> zip(it, it)
>>> [('a', 1), ('b', 2), ('c', 3)]
>>
<snip>
>>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.
<snip>
> 
> Hm, actually, something tells me I've seen some variation of this
> before, but I can't think of the context off hand.
> 
Yes, the subject does come up occasionally. Perhaps you are thinking 
of this thread:

http://groups.google.co.uk/group/comp.lang.python/browse_thread/thread/83baa4bd42fc9b69/d933c7333d3863ce

In that thread, I was the one arguing that the behaviour was undefined.
My memory was that I was forced to back down on that one, but looking back 
at the thread I now think it was only itertools.izip I was forced to admit
defines its behaviour as working that way.

More googling will show that it was proposed that zip should 
be defined as working with this, but that proposal was rejected. See:

http://groups.google.co.uk/group/comp.lang.python/browse_thread/thread/a6ba37b0fb0fa69e/f8a3d3b6d1a9fcbd

So scratch my original suggestion and substitute this for defined behaviour:

>>> it = iter(aList)
>>> list(itertools.izip(it, it))
[('a', 1), ('b', 2), ('c', 3)]




More information about the Python-list mailing list