Files
30-seconds-of-code/snippets/chunk.md
2019-08-20 10:09:53 +03:00

22 lines
427 B
Markdown

---
title: chunk
tags: list
---
Chunks an list into smaller lists of a specified size.
Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `lst`.
```py
from math import ceil
def chunk(lst, size):
return list(
map(lambda x: lst[x * size:x * size + size],
list(range(0, ceil(len(lst) / size)))))
```
```py
chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
```