Skip to main content

Command Palette

Search for a command to run...

Chunk Function in Python

Published
•1 min read•View as Markdown
Chunk Function in Python
A

I am a Tech enthusiast interested to learn new things from everyone and post my learning in public.

Chunks a list into smaller lists of a specified size.

Use range to create a list of desired size. Then use map on this list and fill it with splices of lst.

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)))))
#input and output:
chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]

More from this blog

D

DEVS-OnThe-PUB

18 posts

The Content here will surely impact you, even if its in a small way. A place where Things are without bias...