Python slice a list into chunks

Key takeaways

  • Chunking optimizes memory usage and enhances performance for large datasets, crucial in data analysis and machine learning.

  • Techniques for slicing a list into chunks include:

    • List comprehension: Quick and efficient slicing

    • itertools: Handles varying list lengths with ease

    • Generator functions: Memory-efficient iteration

    • Loop slicing: Simple and clear approach

  • The selection of a method depends on requirements like readability and efficiency.

Chunking, a fundamental technique in Python programming, holds significant importance across various domains. It serves as a cornerstone for efficiently handling large datasets by partitioning them into smaller, more manageable units. This process not only optimizes memory usage but also facilitates streamlined operations in machine learning, data analysis, parallel computing, and real-time data streaming. By breaking down complex datasets into digestible chunks, Python empowers developers to enhance performance, scalability, and flexibility in their applications.

Let’s take a look at the following slides to understand the process of slicing a list into chunks.

canvasAnimation-image
1 of 9

In this Answer, we will learn different methods to slice a list into chunks in Python.

Using list comprehension

One way is to use list comprehension over the list and create chunks of the desired size. Here’s a simple function that slices a list into chunks:

def chunk_list(lst, chunk_size):
return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 3
chunks = chunk_list(my_list, chunk_size)
print(chunks)

Code explanation

  • Line 1: This defines a function named chunk_list that takes two arguments:

      • lst: This is the list to be divided into chunks.

      • chunk_size: This is the desired size of each chunk.

  • Line 2: This returns a new list containing chunks of the original list. This line uses list comprehension for efficiency. The lst[i:i + chunk_size] slices the input list from index i to i + chunk_size, creating a chunk and for i in range(0, len(lst), chunk_size) iterates over indexes of the input list with a step of chunk_size.

  • Line 5: This creates a list of numbers from one to 10.

  • Line 6: This sets the desired chunk size to three.

  • Line 7: This calls the chunk_list function with my_list and chunk_size as arguments, storing the result in the chunks variable.

  • Line 8: This prints the resulting list of chunks to the console.

Using itertools

We can use zip with the * operator along with iter to create chunks.

from itertools import zip_longest
def chunk_list(lst, chunk_size):
args = [iter(lst)] * chunk_size
return list(zip_longest(*args))
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 3
chunks = chunk_list(my_list, chunk_size)
print(chunks)
  • Line 1: This imports the zip_longest function from the itertools module.

  • Line 3: This defines a function named chunk_list that takes two arguments. lst is the list to be divided into chunks. Here, chunk_size is the desired size of each chunk.

  • Line 4: This creates a list of chunk_size iterators, each pointing to the beginning of the input list lst.

  • Line 5: This uses zip_longest to iterate over the created iterators simultaneously. *args unpacks the list of iterators into individual arguments for zip_longest. The zip_longest creates tuples of elements from each iterator, filling missing values with None if iterators have different lengths and converts the resulting iterator to a list and returns it.

  • Line 7: This creates a list of numbers from one to 10.

  • Line 8: This sets the desired chunk size to three.

  • Line 9: This calls the chunk_list function with my_list and chunk_size as arguments, storing the result in the chunks variable.

  • Line 10: This prints the resulting list of chunks to the console.

Using generator function

Another method to slice a list into chunks is to use a generator function that yields chunks of the list.

def chunk_list(lst, chunk_size):
for i in range(0, len(lst), chunk_size):
yield lst[i:i + chunk_size]
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 3
chunks = chunk_list(my_list, chunk_size)
for chunk in chunks:
print(chunk)
  • Line 1: This defines a function named chunk_list that takes two arguments:

      • lst: The list is to be divided into chunks.

      • chunk_size: This is the desired size of each chunk.

  • Line 2: This iterates over indices of the input list with a step of chunk_size.

  • Line 3: This uses the yield keyword to return a generator. It returns a chunk of the list from the index i to i + chunk_size on each iteration.

  • Line 5: This creates a list of numbers from one to 10.

  • Line 6: This sets the desired chunk size to three.

  • Line 7: This calls the chunk_list function, creating a generator object and assigning it to chunks.

  • Line 9: This iterates over the chunks generated by the chunk_list function.

  • Line 10: This prints each chunk to the console.

Using slicing in a loop

This method is similar to list comprehension but implemented using a loop.

def chunk_list(lst, chunk_size):
chunks = []
for i in range(0, len(lst), chunk_size):
chunks.append(lst[i:i + chunk_size])
return chunks
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 3
chunks = chunk_list(my_list, chunk_size)
print(chunks)
  • Line 1: This defines a function named chunk_list that takes two arguments:

      • lst: This is the list to be divided into chunks.

      • chunk_size: This is the desired size of each chunk.

  • Line 2: This initializes an empty list named chunks to store the resulting chunks.

  • Line 3: This iterates over indices of the input list with a step of chunk_size.

  • Line 4: This appends a chunk of the list from index i to i + chunk_size to the chunks list.

  • Line 5: This returns the chunks list containing all the created chunks.

  • Line 7: This creates a list of numbers from one to 10.

  • Line 8: This sets the desired chunk size to three.

  • Line 9: This calls the chunk_list function with my_list and chunk_size as arguments, storing the result in the chunks variable.

  • Line 10: This prints the resulting list of chunks to the console.

Wrap-up

Each of these methods has its own advantages and may be more suitable depending on factors like efficiency, readability, and specific requirements. In conclusion, the ability to slice lists into chunks represents a critical capability in Python, enabling developers to tackle data-intensive tasks with ease and efficiency. Whether it’s processing massive datasets, implementing batch operations, or managing streaming data sources, chunking provides a robust foundation for optimizing resource utilization and enhancing computational performance. As Python continues to evolve as a leading language for data applications, mastering the art of chunking remains essential for building robust and scalable solutions across different domains.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved