Group a list into sequential n-tuples

This function returns a list of n-tuples from a single "flat" list.


There is probably an easier technique that accomplishes the same thing but I've found this function useful in XML data processing.

def group(lst, n):
    """group([0,3,4,10,2,3], 2) => [(0,3), (4,10), (2,3)]
    
    Group a list into consecutive n-tuples. Incomplete tuples are
    discarded e.g.
    
    >>> group(range(10), 3)
    [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
    """
    return zip(*[lst[i::n] for i in range(n)]) 

code.activestate.com ↗