Converting a flat list to a list of tuples
duncan.booth at invalid.invalid
Wed Nov 23 09:44:49 CET 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)]
>>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.
> 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:
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:
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