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 -

lis= [5, 10, 15, 20, 25]
print(lis)

If you execute the above code it produces the following output ?

[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 print the list of elements with equally sized chunks, and they can be solved simply by using the slice operator.

Example

In the below example we have divided 10 numbers into 5 equally sized lists.

lis=[1,2,3,4,5,6,7,8,9,10]
x=0
y=10
for i in range(x,y,2):
   x=i
   print (lis[x:x+2])

The following output is obtained after executing the above program -

[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. 

To put it in an easier way, 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.

lis = [10,20,30,40,50,60,70,80,90,100]
def chunks(l, n):
   for i in range(0, len(l), n):
      yield l[i:i + n]
n = 2
t = list(chunks(lis, n))
print (t)

After running the above code, the following output will be generated ?

[[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. 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. list 'l[i:i+size_of_chunk]' returns each chunk; yield returns the chunks.

def split(l, size_of_chunk):
   for i in range(0, len(l), size_of_chunk):
      yield l[i:i + size_of_chunk]
size_of_chunk = 4
the_list = [23,56,83,19,38,64,92,56]
print('The even size chunk list is as follows:',list(split(the_list, size_of_chunk)))

Output

We get the following output on executing the above code -

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.

lis = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = 3
x = [lis[i:i + n] for i in range(0, len(lis), n)]
print(x)

Output

After running the program, the result will be as follows -

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Using Numpy Module

A general-purpose package for handling arrays is numpy in Python. It offers a multidimensional array object with outstanding speed as well as tools for interacting with these arrays.

Example

We have a split() method of Numpy array which divides a list into chunks of equal size. Here, there are 6 separate chunks.

import numpy as num
list = [23,56,83,19,38,64,92,56]
print('The evenly sized chunk list is:',num.array_split(list, 6))

Output

Following is an output of the above code -

The evenly sized chunk list is: [array([23, 56]), array([83, 19]), array([38]), array([64]), array([92]), array([56])]
Updated on: 2025-04-17T19:02:58+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements