Skip to content

Instantly share code, notes, and snippets.

@gameame
Created February 2, 2009 11:00
Show Gist options
  • Save gameame/56878 to your computer and use it in GitHub Desktop.
Save gameame/56878 to your computer and use it in GitHub Desktop.
Iterates over the elements of list grouping elements by the specified amount.
def group_iterate(list = [], group_by = 1):
"""
Iterates over the elements of list grouping elements by the specified amount.
Example usage:
>>> for a in group_iterate(range(12), 3):
... print a
...
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9, 10, 11)
"""
for i in zip(*[list[i::group_by] for i in xrange(group_by)]):
yield i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment