Skip to content

Instantly share code, notes, and snippets.

Embed
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.
#!/usr/bin/python
def partition(thelist, n):
try:
n = int(n)
list(thelist)
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.