Converting a flat list to a list of tuples

bonono at gmail.com bonono at gmail.com
Tue Nov 22 18:43:52 EST 2005


Bengt Richter wrote:
> On Tue, 22 Nov 2005 13:37:06 +0100, =?ISO-8859-1?Q?Andr=E9?= Malo <auch-ich-m at g-kein-spam.com> wrote:
>
> >* Duncan Booth <duncan.booth at invalid.invalid> wrote:
> >
> >> 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.




More information about the Python-list mailing list