Created
August 8, 2016 09:33
-
-
Save brainyfarm/39c2604ffbc494a8a161e7cfb2b2d30b to your computer and use it in GitHub Desktop.
FreeCodeCamp: Chunk an Array (Python)
This file contains hidden or 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
| """ | |
| 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