Created
April 22, 2019 14:01
-
-
Save grafst/991bdb187b879b64d1726e8b8bf4d9da to your computer and use it in GitHub Desktop.
split the list into at most n chunks of approximately same size
This file contains 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/python3 | |
def split_into(list,n): | |
chunks=[[] for x in range(min(n,len(list)))] | |
# ^--- replace the 'min(...)' with just 'n' for empty lists if len(list)<n | |
for item in list: | |
chunks[list.index(item)%n].append(item) | |
return chunks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment