generate tuples from sequence

Peter Otten __peter__ at web.de
Thu Jan 18 02:46:32 EST 2007


Alan Isaac wrote:

> "Peter Otten" <__peter__ at web.de> wrote in message
> news:eol9ac$v3l$02$1 at news.t-online.com...
>> I like
>>
>> >>> items = range(9)
>> >>> N = 3
>> >>> zip(*[iter(items)]*N)
>> [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
> 
> Except that it is considered implementation dependent:
> http://mail.python.org/pipermail/python-list/2005-February/307550.html

Use itertools.izip() then instead of zip(). I think that the occurence of
this idiom on the examples page counts as a blessing :-)

But still, to help my lack of fantasy -- what would a sane zip()
implementation look like that does not guarantee the above output?

Peter




More information about the Python-list mailing list