Skip to content

Instantly share code, notes, and snippets.

@Dleau
Created December 17, 2019 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dleau/fd6107884fc0d91d9496dc0701f98aac to your computer and use it in GitHub Desktop.
Save Dleau/fd6107884fc0d91d9496dc0701f98aac to your computer and use it in GitHub Desktop.
Split list into "chunks"
# split list l into c "chunks"
def split_into_chunks(l, c):
chunks = []
for i in range(0, c):
chunks.append([])
for i, e in enumerate(l):
chunks[i % c].append(e)
return chunks
l = [1, 2, 3, 4, 5, 6]
print(split_into_chunks(l, 1))
print(split_into_chunks(l, 2))
print(split_into_chunks(l, 3))
print(split_into_chunks(l, 6))
'''
[[1, 2, 3, 4, 5, 6]]
[[1, 3, 5], [2, 4, 6]]
[[1, 4], [2, 5], [3, 6]]
[[1], [2], [3], [4], [5], [6]]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment