Created
November 16, 2020 11:58
-
-
Save terngkub/c7c60a57c30a748b218186be85bc2aea to your computer and use it in GitHub Desktop.
Python: split a list into a list of chunks
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
| def split_list(lst, chunk_nb): | |
| list_size = len(lst) | |
| ret = [] | |
| for i in range(chunk_nb): | |
| beg = round(list_size * i / chunk_nb) | |
| end = round(list_size * (i+1) / chunk_nb) | |
| ret.append(lst[beg:end]) | |
| return ret | |
| splits = split_list(list(range(10)), 3) | |
| for split in splits: | |
| print(split) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment