Converting a flat list to a list of tuples

George Sakkis gsakkis at rutgers.edu
Tue Nov 22 10:42:31 EST 2005


"Laurent Rahuel" wrote:

> Hi,
>
> newList = zip(aList[::2], aList[1::2])
> newList
> [('a', 1), ('b', 2), ('c', 3)]
>
> Regards,
>
> Laurent

Or if aList can get very large and/or the conversion has to be
performed many times:

from itertools import islice
newList = zip(islice(aList,0,None,2), islice(aList,1,None,2))

George




More information about the Python-list mailing list