here2share

# chunk_into_n.py

Jul 20th, 2022 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. # chunk_into_n.py
  2.  
  3. from math import ceil
  4.  
  5. def chunk_into_n(lst, n):
  6.   size = ceil(len(lst) / n)
  7.   return list(
  8.     map(lambda x: lst[x * size:x * size + size],
  9.     list(range(n)))
  10.   )
  11. ttt = [1, 2, 3, 4, 5, 6, 7]
  12. print( chunk_into_n(ttt, 4) ) # [[1, 2], [3, 4], [5, 6], [7]]
  13. print( chunk_into_n(ttt, 3) ) # [[1, 2, 3], [4, 5, 6], [7]]
Add Comment
Please, Sign In to add comment