How do you split a list into evenly sized chunks in Python?


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

Example

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

Output

If you execute the above snippet, it produces the following output.

[5, 10, 15, 20, 25]

Let us discuss various ways to split a list into evenly sized chunks in Python.

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])

Output

The following output is obtained after executing the above program.

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

Using 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 existing the function and without losing it local states we use 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)

Output

On executing the above program, the following output is generated.

[[10, 20], [30, 40], [50, 60], [70, 80], [90, 100]]

Example 2

We 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 evenly sized chunk list is:',list(split(the_list, size_of_chunk)))

Output

We get the following output on executing the above code −

The evenly sized chunk list is: [[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

On executing the above program, the following output is generated.

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

Numpy's array split() method 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: 08-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements