Created
December 17, 2019 04:10
-
-
Save Dleau/fd6107884fc0d91d9496dc0701f98aac to your computer and use it in GitHub Desktop.
Split list into "chunks"
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
# 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