Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Break a list into 'n' pieces. The last list may be larger than the rest if the list doesn't break cleanly.
def partition(thelist, n):
n = int(n)
except (ValueError, TypeError):
return [thelist]
p = len(thelist) / n
return [thelist[p*i:p*(i+1)] for i in range(n - 1)] + [thelist[p*(i+1):]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.