Skip to content

Instantly share code, notes, and snippets.

@grafst
Created April 22, 2019 14:01
Show Gist options
  • Save grafst/991bdb187b879b64d1726e8b8bf4d9da to your computer and use it in GitHub Desktop.
Save grafst/991bdb187b879b64d1726e8b8bf4d9da to your computer and use it in GitHub Desktop.
split the list into at most n chunks of approximately same size
#!/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