Converting a flat list to a list of tuples

Bengt Richter bokr at oz.net
Tue Nov 22 15:03:16 EST 2005


On 22 Nov 2005 11:11:23 GMT, 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)]
>
Thank you for that. That is cool ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list