Converting a flat list to a list of tuples

Laurent Rahuel lrahuel.notgood at voila.fr
Tue Nov 22 12:33:12 CET 2005


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]]
> 
> I was thinking of something along the lines of:
> for (key,number) in list:
>   print key, number
> 
> but it's not working...
> 
> Thank you

Hi,

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

Regards,

Laurent.



More information about the Python-list mailing list