iterator question

Boris Borcic bborcic at gmail.com
Tue Sep 26 13:58:52 EDT 2006


Neal Becker wrote:
> Any suggestions for transforming the sequence:
> 
> [1, 2, 3, 4...]
> Where 1,2,3.. are it the ith item in an arbitrary sequence
> 
> into a succession of tuples:
> 
> [(1, 2), (3, 4)...]
> 
> In other words, given a seq and an integer that specifies the size of tuple
> to return, then for example:

 >>> trf = lambda iterable,n : zip(*[iter(iterable)]*n)
 >>> trf(range(15),3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]


note though that it will silently drop any remainder.

hth, bb



More information about the Python-list mailing list