Converting a flat list to a list of tuples

Peter Otten __peter__ at web.de
Wed Nov 23 04:01:54 EST 2005


bonono at gmail.com wrote:

> Personally, I would like to see it as [('a',1,'b',2), ('c',3,
> None,None)],  as a list of tuple of equal length is easier to be dealt
> with.
> 
> i = iter(aList)
> zip(i,chain(i,repeat(None)),
> chain(i,repeat(None)),chain(i,repeat(None)))

Here's some more:

>>> it = iter(range(5))
>>> map(None, it, it)
[(0, 1), (2, 3), (4, None)]

>>> N = 3
>>> it = chain(range(10), repeat("MISSING", N-1))
>>> zip(*(it,)*N)
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 'MISSING', 'MISSING')]

Peter




More information about the Python-list mailing list