Skip to content

Instantly share code, notes, and snippets.

@brainyfarm
Created August 8, 2016 09:33
Show Gist options
  • Save brainyfarm/39c2604ffbc494a8a161e7cfb2b2d30b to your computer and use it in GitHub Desktop.
Save brainyfarm/39c2604ffbc494a8a161e7cfb2b2d30b to your computer and use it in GitHub Desktop.
FreeCodeCamp: Chunk an Array (Python)
"""
Write a function that splits an array (first argument) into
groups the length of size (second argument) and returns them as a two-dimensional array.
"""
def chunk_list(the_list, size):
m = size
index = 0
big_list = []
while(size <= len(the_list) +m):
to_append = the_list[index:size]
if(len(to_append) > 0):
big_list.append(to_append)
size += m
index += m
return big_list
print chunk_list([0, 1, 2, 3, 4, 5,6], 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment