def take_while(fn, coll): """Yield values from coll until fn is False""" for e in coll: if fn(e): yield e else: return def partition(n, coll, step=None): return take_while(lambda e: len(e) == n, (coll[i:i+n] for i in xrange(0, len(coll), step or n))) def partition_all(n, coll, step=None): return (coll[i:i+n] for i in xrange(0, len(coll), step or n))