Collecting data into chunks in Python

This is how to split an iterable into fixed-length chunks or blocks.
If the number of items in the iterable is not a multiple of the block size, the last block is padded with a fill value.

Method 1: A generator function

def grouper(iterable, n, fillvalue=None):
    for i in range(0, len(iterable), n):
        block = [j for j in iterable[i:i + n]] 
        yield tuple(block + [fillvalue] * (n - len(block)))

Method 2: Use itertools.izip_longest()

This is from the itertools documentation

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Example Program

    alphabet = "".join(chr(c) for c in range(65, 91)) # ABC...
    print list(grouper(alphabet, 3))


[('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'H', 'I'), ('J', 'K', 'L'), ('M', 'N', 'O'), ('P', 'Q', 'R'), ('S', 'T', 'U'), ('V', 'W', 'X'), ('Y', 'Z', None)]