Last active
March 27, 2016 01:07
-
-
Save craigderington/c368efec90e249771777 to your computer and use it in GitHub Desktop.
Break a list into 'n' pieces. The last list may be larger than the rest if the list doesn't break cleanly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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