Converting a flat list to a list of tuples

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Nov 22 06:50:40 EST 2005


On Tue, 22 Nov 2005 11:11:23 +0000, Duncan Booth wrote:

>>>> aList = ['a', 1, 'b', 2, 'c', 3]
>>>> it = iter(aList)
>>>> zip(it, it)
> [('a', 1), ('b', 2), ('c', 3)]

I'm not sure if I should fall to my knees in admiration of a Cool Hack,
or recoil in horror at a Bogus Kludge :-)

The code looks like it should return [('a', 'a'), (1, 1), ('b', 'b'), (2,
2), ('c', 'c'), (3, 3)] but of course it does not: the arguments for zip
are not independent.

I guess it is more of a Neat Trick, with a dash of Gotcha For The Unwary.



-- 
Steven.




More information about the Python-list mailing list