# a sequence question

Nick Coghlan ncoghlan at iinet.net.au
Fri Jan 28 20:09:00 EST 2005

```Duncan Booth wrote:
> Try this:
>
> l = [1, 2, 3, 4]
> for a, b in zip(*[iter(l)]*2):
>    print a, b
>
> zip(*[iter(seq)]*N) will group by N (but if there are any odd items at the
> end it will ignore them).
>
> map(None, *[iter(seq)]*N) will group by N padding the last item with None
> if it needs to.

For anyone else who was as bemused as I was that Duncan's and F. Petitjean's
suggestions actually *work*, this was what I had to do to figure out *why* they
work:

Py> l = [1, 2, 3, 4]
Py> itr = iter(l)
Py> zip(itr) # Put all items from iterator in position 1
[(1,), (2,), (3,), (4,)]
Py> itr = iter(l)
Py> zip(itr, itr) # Put every second item in position 2
[(1, 2), (3, 4)]

Using zip(*[iter(l)]*N) or zip(*(iter(l),)*N) simply extends the above to the
general case.

I'd definitely recommend hiding this trick inside a function. Perhaps something
like (using Michael's function name):

from itertools import izip, repeat, chain

def partition(seq, part_len):
return izip(*((iter(seq),) * part_len))

itr = iter(seq)
if (len(seq) % part_len != 0):
return izip(*((itr,) * part_len))

Py> list(partition(range(10), 2))
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
Py> list(partition(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, False, False)]
[(0, 3, 6, 9), (1, 4, 7, None), (2, 5, 8, None)]

Not sure how useful that last example is, but I thought it was cute :)

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net

```