Return to Snippet

Revision: 13818
at May 7, 2009 03:15 by iamok


Updated Code
def g(l, n):
    return zip(*[iter(l)]*n)

'''
>>> g(range(10), 3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
>>> g('hello world!', 2)
[('h', 'e'), ('l', 'l'), ('o', ' '), ('w', 'o'), ('r', 'l'), ('d', '!')]

Yeah, course. g groups a list l into a list of n-tuples, by taking each group of n elements from the list and making them into a tuple. How useful.
'''

Revision: 13817
at May 7, 2009 03:14 by iamok


Initial Code
def g(l, n):
    return zip(*[iter(l)]*n)

Initial URL
http://drj11.wordpress.com/2009/01/28/my-python-dream-about-groups/#

Initial Description

                                

Initial Title
grouping items in a sequence

Initial Tags

                                

Initial Language
Python