Converting a flat list to a list of tuples

bonono at gmail.com bonono at gmail.com
Tue Nov 22 08:00:09 EST 2005


André Malo 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?
>
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.




More information about the Python-list mailing list