Menu

gitpiper

chunk python Code Snippet in 2024

listintermediate

Last Updated: 20 April 2024

Chunks a list into smaller lists of a specified size.

  • Use list() and range() to create a list of the desired size.
  • 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]]

python snippet similar to chunk For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️