Skip to content

Instantly share code, notes, and snippets.

@vitalysim
Last active January 13, 2019 18:51
Show Gist options
  • Save vitalysim/7d937fe379d4a8fcd4af2a97f024b462 to your computer and use it in GitHub Desktop.
Save vitalysim/7d937fe379d4a8fcd4af2a97f024b462 to your computer and use it in GitHub Desktop.
# Divide list into chunks containing n element
def divide_chunks(l, n_elements):
for i in range(0, len(l), n_elements):
yield l[i:i + n_elements]
if __name__ == "__main__":
n = [1,2,3,4,5,6,7,8,9,10,12,13,14,15]
for chunck in divide_chunks(n, 5):
print(chunck)
# Output:
# [1, 2, 3, 4, 5]
# [6, 7, 8, 9, 10]
# [12, 13, 14, 15]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment