FAQ
Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.

## Search Discussions

•  at May 14, 2009 at 1:35 pm ⇧

Neal Becker ha scritto:
Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.
http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python

Gla
•  at May 14, 2009 at 2:30 pm ⇧

Neal Becker wrote:
Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.
This is my best effort... not using itertools as my brain doesn't seem
to work that way!

class Grouper(object):
def __init__(self, n, i):
self.n = n
self.i = iter(i)
def __iter__(self):
while True:
out = tuple(self.i.next() for _ in xrange(self.n))
if not out:
break
yield out

g = Grouper(5, xrange(20))
print list(g)

g = Grouper(4, xrange(19))
print list(g)

Which produces

[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14), (15, 16, 17, 18, 19)]
[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15), (16, 17, 18)]

--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
•  at May 14, 2009 at 2:40 pm ⇧

Neal Becker wrote:

Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.
Depending on what you want to do with items that don't make a complete N-
tuple:
from itertools import *
items = range(10)
list(izip(*(iter(items),)*3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
list(izip_longest(*(iter(items),)*3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]
list(takewhile(bool, imap(tuple, starmap(islice, repeat((iter(items),
3))))))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]

Peter
•  at May 14, 2009 at 3:38 pm ⇧

Neal Becker wrote:
Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.

An option, might be better since it handles infinite list correctly:
lst = [1, 4, 2, 5, 7, 3, 2, 5, 7, 3, 2, 6, 3, 2, 6, 8, 4, 2]
d = 4
for x in itertools.groupby(enumerate(lst), lambda x: x[0] // d):
... print(list(x[1]))
...
[(0, 1), (1, 4), (2, 2), (3, 5)]
[(4, 7), (5, 3), (6, 2), (7, 5)]
[(8, 7), (9, 3), (10, 2), (11, 6)]
[(12, 3), (13, 2), (14, 6), (15, 8)]
[(16, 4), (17, 2)]
[list(x[1]) for x in itertools.groupby(enumerate(lst), lambda x:
x[0] // d)]
[[(0, 1), (1, 4), (2, 2), (3, 5)], [(4, 7), (5, 3), (6, 2), (7, 5)],
[(8, 7), (9, 3), (10, 2), (11, 6)], [(12, 3), (13, 2), (14, 6), (15,
8)], [(16, 4), (17, 2)]]
•  at May 14, 2009 at 3:53 pm ⇧
In article <guh5qc\$lsc\$1 at ger.gmane.org>,
Neal Becker wrote:
Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.
This topic was discussed here just a few days ago:

--
Ned Deily,
•  at May 14, 2009 at 4:00 pm ⇧

On Thu, May 14, 2009 at 8:53 AM, Ned Deily wrote:
In article <guh5qc\$lsc\$1 at ger.gmane.org>,
?Neal Becker wrote:
Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.
This topic was discussed here just a few days ago:

They really should just add grouper() to itertools rather than leaving
it as a recipe. People keep asking for it so often...

Cheers,
Chris
•  at May 14, 2009 at 5:06 pm ⇧

Chris Rebert wrote:
They really should just add grouper() to itertools rather than leaving
it as a recipe. People keep asking for it so often...
I've just added it to the issue tracker: http://bugs.python.org/issue6021
•  at May 15, 2009 at 12:03 pm ⇧

Neal Becker wrote:

Is there any canned iterator adaptor that will

transform:
in = [1,2,3....]

into:
out = [(1,2,3,4), (5,6,7,8),...]

That is, each time next() is called, a tuple of the next N items is
returned.
Here's one that abuses a for loop:

from itertools import islice

def grouper(seq,n):
it = iter(seq)
for x in it:
yield (x,) + tuple(islice(it,n-1))

def test():
L = range(11)
n = 3
for x in grouper(L,n):
print x

if __name__ == '__main__':
test()

BTW what's up with the followup to gmane?

P.

## Related Discussions

Discussion Overview
 group python-list categories python posted May 14, '09 at 1:20p active May 15, '09 at 12:03p posts 9 users 8 website python.org

### 8 users in discussion

Content

People

Support

Translate

site design / logo © 2018 Grokbase