def partition(lst, n):
    q, r = divmod(len(lst), n)
indices = [(q * i + min(i, r)) for i in range(n + 1)]
return [lst[indices[i]:indices[i + 1]] for i in range(n)]
Python: Slicing a list into n nearly-equal-length partitions