Split List into chunks of size N in Python

In this article, we will discuss different ways to split a list into multiple lists of given size. If length of list is not completely divisible by the chunk size, then last sublist will have less elements.

Table Of Contents

Introduction

Suppose we have a list of numbers,

listOfNumbers = [11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22]

Now we want to break this list into multiple lists of chunk size 4. These lists will be,

[11, 12, 13, 14]

[16, 17, 18, 19]

[20, 21, 22]

As list had 11 elements only, which is not completely divisible by 4, therefore last sublist has 3 elements only.

There are different ways to split a list based on chunk size. Let’s discuss them one by one,

Method 1: Using for loop

We have created a function splitInChunks(), to split a list into multiple lists of given size. It accepts a list and chunk size as arguments. Then, it iterates over a range of numbers from 0 till the Size of List, with step size as the given chunk size. Then it slices the list elements from i till i + chunkSize, where i is the starting point of each chunk. Then, it yields the sliced sublists one by one. Let’s see an example, where we will split a list into chunks of size 4.

# Split a given list to multiple lists of given size
# and yield them one by one
def splitInChunks(listObj, chunkSize):
    for i in range(0, len(listObj), chunkSize):
        yield listObj[ i : i + chunkSize]


listOfNumbers = [11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22]

chunkSize = 4
# Split the list into sublists of size 4
for subList in splitInChunks(listOfNumbers, chunkSize):
    print(subList)

Output:

[11, 12, 13, 14]
[16, 17, 18, 19]
[20, 21, 22]

As list had 11 elements only, which is not completely divisible by 4, therefore last sublist has 3 elements only. All other sub lists has 4 eleemnts each.

Method 2: Using List Comprehension

We can follow the same logic as in the previous technique, but instead of a for loop, we are going to use the List comprehension. Inside the List Comprehension, iterate over a range of numbers from 0 till the Size of List, with step size as the given chunk size. Then slice the list elements from i till i + chunkSize, where i is the starting point of each chunk, and append each slice to a new list. Let’s see an example, where we will split a list into chunks of size 4.

listOfNumbers = [11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22]

chunkSize = 4

# Split a given list to multiple lists of size 4 each
subLists = [listOfNumbers[ i : i + chunkSize] for i in range(0, len(listOfNumbers), chunkSize)]

for subList in subLists:
    print(subList)

Output:

[11, 12, 13, 14]
[16, 17, 18, 19]
[20, 21, 22]

As list had 11 elements only, which is not completely divisible by 4, therefore last sublist has 3 elements only. All other sub lists has 4 eleemnts each.

Method 3: Using NumPy

NumPy Module provides a function array_split(). It takes a sequence and an int N as arguments. Then splits the sequence into N multiple sub-arrays and returns them. We can calculate the number of sublists required by dividing the size of list by the given chunk size. Then pass the list and number of sublists as arguments to the array_split(). It will return a sub arrays of given chunk size. Let’s see an example,

import numpy as np
import math

listOfNumbers = [11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22]

chunkSize = 4

# get number of lists based on chunk size
numberOfSubLists = math.ceil(len(listOfNumbers) / chunkSize)

# Split a given list to multiple lists of size 4 each
for subArray in np.array_split(listOfNumbers, numberOfSubLists):
    subList = subArray.tolist()
    print(subList)

Output:

[11, 12, 13, 14]
[16, 17, 18, 19]
[20, 21, 22]

As list had 11 elements only, which is not completely divisible by 4, therefore last sublist has 3 elements only. All other sub lists has 4 eleemnts each.

Summary

We learned how to split a list into multiple lists of given chunk size in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top