Chunk List

Description: Chunk a list into separate lists of a specific max size.
Tested Platform: Python 3.11
Language: Python
"""
  Chunk a list in list of chunks of a specific size

  e.g. Chunksize = 2 -> [1, 2, 3, 4, 5] -> [[1,2], [3,4], [5]]
"""

def chunk_list(list, chunkSize):
    return [list[i:i + chunkSize] for i in range(0, len(list), chunkSize)]

Posted: March 20, 2023

Return to the snippets listing