Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do you split a list into evenly sized chunks in Python?
Sometimes it is necessary to divide a lengthy list into smaller and easy-to-read data. For example, if you wish to arrange a list of items in groups, it can be helpful to break it into small parts. This is useful for tasks like grouping data for analysis or showing items in a user interface.
Python provides various simple methods to do this. In order to work with smaller data sets without losing any information, this article will show you how to split a list into uniformly sized chunks.
What is List?
List is one of the frequently used data structures in python. A list is a data structure in python that is mutable and has ordered sequence of elements. Following is a list of integer values ?
numbers = [5, 10, 15, 20, 25] print(numbers)
[5, 10, 15, 20, 25]
Here we can split a list into evenly sized chunks in Python using different ways ?
Using Slice Operator
You can split a list into equally sized chunks using the slice operator in a simple loop approach.
Example
In the below example we have divided 10 numbers into 5 equally sized lists ?
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 2
for i in range(0, len(numbers), chunk_size):
chunk = numbers[i:i + chunk_size]
print(chunk)
[1, 2] [3, 4] [5, 6] [7, 8] [9, 10]
Using the Yield Keyword
Yield is a Python keyword which is used to return from a function, where it does not forget its local states. When we want to have multiple returns (partial solutions) from a function without exiting the function and without losing its local states we use the yield keyword.
Example 1
The following is an example program to demonstrate the usage of yield keyword to split a list into evenly sized chunks in python ?
def chunks(data, n):
for i in range(0, len(data), n):
yield data[i:i + n]
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
chunk_size = 2
result = list(chunks(numbers, chunk_size))
print(result)
[[10, 20], [30, 40], [50, 60], [70, 80], [90, 100]]
Example 2
Here we have defined a function to split the list in the example below. We iterate from 0 to the length of the list using the for loop and range() method, using the size of the chunk as the step ?
def split_list(data, size_of_chunk):
for i in range(0, len(data), size_of_chunk):
yield data[i:i + size_of_chunk]
size_of_chunk = 4
the_list = [23, 56, 83, 19, 38, 64, 92, 56]
result = list(split_list(the_list, size_of_chunk))
print('The even size chunk list is as follows:', result)
The even size chunk list is as follows: [[23, 56, 83, 19], [38, 64, 92, 56]]
Using List Comprehension
List comprehension provides shorter syntax and allows to create new lists from other iterables like tuples, lists, strings, arrays etc.
Example
The following is an example program to split a list into evenly sized chunks in python using the list comprehension ?
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunk_size = 3 chunks = [numbers[i:i + chunk_size] for i in range(0, len(numbers), chunk_size)] print(chunks)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Using Numpy Module
NumPy is a general-purpose package for handling arrays in Python. It offers a multidimensional array object with outstanding speed as well as tools for interacting with these arrays.
Example
We have a array_split() method of NumPy which divides a list into chunks. Here, we split the list into 4 chunks ?
import numpy as np
numbers = [23, 56, 83, 19, 38, 64, 92, 56]
chunks = np.array_split(numbers, 4)
print('The evenly sized chunk list is:', chunks)
The evenly sized chunk list is: [array([23, 56]), array([83, 19]), array([38, 64]), array([92, 56])]
Comparison
| Method | Memory Efficient | Readability | Best For |
|---|---|---|---|
| Slice Operator | Medium | Good | Simple splitting tasks |
| Yield Keyword | High | Good | Large lists, memory-efficient processing |
| List Comprehension | Medium | Excellent | Concise one-liner solutions |
| NumPy | High | Good | Numerical data, array operations |
Conclusion
Python offers multiple approaches to split lists into evenly sized chunks. Use list comprehension for readable one-liners, yield for memory-efficient processing of large lists, and NumPy for numerical data operations.
