Created
February 2, 2009 11:00
-
-
Save gameame/56878 to your computer and use it in GitHub Desktop.
Iterates over the elements of list grouping elements by the specified amount.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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