Update formatting in every_nth Update formatting in shuffle Update formatting in has_duplicates Update formatting in group_by Update formatting in sum_by Update formatting in zip Update formatting in longest_item Update formatting in bifurcate_by Update formatting in difference_by Update formatting in clamp_number Update formatting in min_by Update formatting in max_by Update formatting in union Update formatting in n_times_string Update formatting in check_prop Update formatting in chunk Update formatting in transpose Update formatting in bifurcate Update formatting in union_by Update formatting in initialize_list_with_range Update formatting in most_frequent
491 B
491 B
title, tags
| title | tags |
|---|---|
| chunk | list,intermediate |
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(0, ceil(len(lst) / size)))))
chunk([1, 2, 3, 4, 5], 2) # [[1,2],[3,4],5]