596 B
596 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Split list into chunks | snippet | python |
|
red-berries | 2020-11-02T19:27:07+02:00 |
Chunks a list into smaller lists of a specified size.
- Use
list()andrange()to create a list of the desiredsize. - Use
map()on the list and fill it with splices of the given list. - Finally, return the created list.
from math import ceil
def chunk(lst, size):
return list(
map(lambda x: lst[x * size:x * size + size],
list(range(ceil(len(lst) / size)))))
chunk([1, 2, 3, 4, 5], 2) # [[1, 2], [3, 4], [5]]