Python function for developers
Add docstring + type hints to small Python utility
See input + output preview
Input
- language
- python
- code
- def chunk_list(lst, n): return [lst[i:i+n] for i in range(0, len(lst), n)]
Output (excerpt)
```python
def chunk_list(lst, n):
"""
Split a list into consecutive sublists of length ``n``.
The final sublist may be shorter than ``n`` if ``len(lst)`` is not
an exact multiple of ``n``. Order is preserved.
Args:
lst: Source iterable, materialized into a list-like sequence.
n: Chunk size. Must be a positive integer.
Returns:
A list of lists, where each inner list is a contiguous slice of ``lst``.
Example:
>>> chunk_list([1, 2, 3, 4, 5], 2)
[[1, 2], [3, 4], [5]]
"""
return [lst[i:i+n] for i in range(0, len(lst), n)]
```