Tag Archives: itertools

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))

Output

[('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)]

How to flatten a list of lists in Python

If you have a list of lists like this:

lol = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

And you want to flatten it into a single list like this:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

You can use a list comprehension like this:

l = [item for sublist in lol for item in sublist]

Or you can use itertools.chain() like this:

from itertools import chain
l = list(chain(*lol))