# Python Split list into chunks

In this tutorial, you will learn how to split a list into chunks in Python using different ways with examples.

## Python Split list into chunks

Lists are mutable and heterogenous, meaning they can be changed and contain different data types. We can access the elements of the list using their index position.

There are five various ways to split a list into chunks.

1. Using a For-Loop
2. Using the List Comprehension Method
3. Using the itertools Method
4. Using the NumPy Method
5. Using the lambda Function

## Method 1: Using a For-Loop

The naive way to split a list is using the for loop with help of `range()` function.

The range function would read `range(0, 10, 2)`, meaning we would loop over the items `0,2,4,6,8`.

We then index our list from `i:i+chunk_size`, meaning the first loop would be `0:2`, then `2:4`, and so on.

``````# Split a Python List into Chunks using For Loops

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunked_list = list()
chunk_size = 2
for i in range(0, len(sample_list), chunk_size):
chunked_list.append(sample_list[i:i+chunk_size])
print(chunked_list)
``````

Output

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

## Method 2: Using the List Comprehension Method

The list comprehension is an effective way to split a list into chunks when compared to for loop, and it’s more readable.

We have a` sample_list` and contain ten elements in it. We will split the list into equal parts with a `chunk_size` of 2.

``````# Split a Python List into Chunks using list comprehensions
sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size=2
result=[sample_list[i:i + chunk_size] for i in range(0, len(sample_list), chunk_size)]
print(result)
``````

Output

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

`sample_list[i:i + chunk_size]` give us each chunk. For example, if i=0, the items included in the chunk are `i to i+chunk_size`, which is from `0:2` index. In the next iteration, the items included would be `2:4` index and so on.

## Method 3: Using the itertools Method

We can leverage the `itertools `module to split a list into chunks. The `zip_longest()` function returns a generator that must be iterated using for loop. It’s a straightforward implementation and returns a list of tuples, as shown below.

``````# Split a Python List into Chunks using itertools

from itertools import zip_longest

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size=2
result=list(zip_longest(*[iter(sample_list)]*chunk_size, fillvalue=''))
print(result)

chunked_list = [list(item) for item in list(zip_longest(*[iter(sample_list)]*chunk_size, fillvalue=''))]
print(chunked_list)``````

Output

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

## Method 4: Using the NumPy Method

We can use the `NumPy `library to divide the list into n-sized chunks. The `array_split()` function splits the list into sublists of specific size defined as n.

``````# Split a Python List into Chunks using numpy

import numpy as np

# define array and chunk_szie
sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
our_array = np.array(sample_list)
chunk_size = 2

# split the array into chunks
chunked_arrays = np.array_split(our_array, len(sample_list) / chunk_size)
print(chunked_arrays)

# Covert chunked array into list
chunked_list = [list(array) for array in chunked_arrays]
print(chunked_list)``````

Output

``````[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8]), array([ 9, 10])]
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]``````

## Method 5: Using the lambda Method

We can use the lambda function to divide the list into chunks. The lambda function will iterate over the elements in the list and divide them into N-Sized chunks, as shown below.

``````# Split a Python List into Chunks using lambda function

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

lst= lambda sample_list, chunk_size: [sample_list[i:i+chunk_size] for i in range(0, len(sample_list), chunk_size)]
result=lst(sample_list, chunk_size)
print(result)
``````

Output

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

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

## [Solved] ImportError: No module named matplotlib.pyplot

Table of Contents Hide ImportError: No module named matplotlib.pyplotInstall Matplotlib in OSX/Linux Install Matplotlib in WindowsInstall Matplotlib in Anaconda The ImportError: No module named matplotlib.pyplot occurs if you have not installed…

## [Solved] RuntimeWarning: invalid value encountered in true_divide

Table of Contents Hide What is RuntimeWarning: invalid value encountered in true_divide?How to fix RuntimeWarning: invalid value encountered in true_divide?Conclusion Suppose you attempt to divide the NumPy arrays elements using…

## How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…

## List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…

## How to get hostname in Python?

Table of Contents Hide Python code to get a hostname using the socket modulePython code to find a hostname using the platform modulePython code to find a hostname using the…

## Python IndexError: list index out of range

Table of Contents Hide The Problem: IndexError: list index out of rangeThe solution to Indexerror: list index out of rangeSolution 1 – Using len() functionSolution 2 – Using for loop…