Partition implemented in Python

By | March 16, 2018

Coming from a functional programming mindset, I needed a partition function in Python. I discovered this on the internet and wanted to share it here with anyone else looking for similar functions.

You can see what partition typically does at ClojureDocs to get an idea if you are curious.


def partition(n, step, coll):
    for i in range(0, len(coll), step):
        if (i+n > len(coll)):
            break #  raise StopIteration...
        yield coll[i:i+n]