How to split a Python list into evenly sized chunks Last Updated : 04 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Iteration in Python is repeating a set of statements until a certain condition is met. This is usually done using a for loop or a while loop. There are several ways to split a Python list into evenly sized-chunks. Here are the 5 main methods: Method 1: Using a Loop with List SlicingUse for loop along with list slicing to iterate over chunks of a list. Python def chunked_list(lst, chunk_size): for i in range(0, len(lst), chunk_size): yield lst[i:i + chunk_size] if __name__ == "__main__": lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunk_size = 3 for chunk in chunked_list(lst, chunk_size): print(chunk) Output[1, 2, 3] [4, 5, 6] [7, 8, 9] Method 2: Using List Comprehension with range()You can also use a list comprehension with range() to create the chunks and then iterate over them. This is the one line code rather than using yield. Python lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunk_size = 3 chunks = [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)] for chunk in chunks: print(chunk) Output[1, 2, 3] [4, 5, 6] [7, 8, 9] Method 3: Using itertools.islice()You can also use islice function, which is provided by the the itertools module. It is used to create an iterator that returns selected elements from the iterable. Python from itertools import islice def chunked_list(lst, chunk_size): it = iter(lst) while True: chunk = list(islice(it, chunk_size)) if not chunk: break yield chunk if __name__ == "__main__": lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunk_size = 3 for chunk in chunked_list(lst, chunk_size): print(chunk) Output[1, 2, 3] [4, 5, 6] [7, 8, 9] Method 4: Using NumPy (for large arrays/lists)While, you will be dealing with large lists or need high performance, using numpy is the best idea. Python import numpy as np def chunked_list(lst, chunk_size): return np.array_split(lst, np.ceil(len(lst) / chunk_size)) if __name__ == "__main__": lst = [1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18] chunk_size = 4 chunks = chunked_list(lst, chunk_size) for chunk in chunks: print(chunk) Output[1 2 3 4] [5 6 7 8] [ 9 10 11 12] [13 14 15] [16 17 18] Method 5: Using a Generator FunctionThis Generator method is similar to list comprehension but more memory-efficient as it yields chunks one at a time. Python def chunked_list(list_data,chunk_size): for i in range(0,len(list_data),chunk_size): yield list_data[i:i + chunk_size] if __name__ == "__main__": lst = [11,12,13,14,15,16,17,18,19,20,21,22] chunk_size = 3 for chunk in chunked_list(lst, chunk_size): print(chunk) Output[11, 12, 13] [14, 15, 16] [17, 18, 19] [20, 21, 22] Comment More infoAdvertise with us Next Article How to split a Python list into evenly sized chunks S somraj210sbk Follow Improve Article Tags : Python Practice Tags : python Similar Reads How to Split a File into a List in Python In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples 5 min read Break a List into Chunks of Size N in Python The goal here is to break a list into chunks of a specific size, such as splitting a list into sublists where each sublist contains n elements. For example, given a list [1, 2, 3, 4, 5, 6, 7, 8] and a chunk size of 3, we want to break it into the sublists [[1, 2, 3], [4, 5, 6], [7, 8]]. Letâs explor 3 min read How to Read Text File Into List in Python? In Python, reading a text file into a list is a common task for data processing. Depending on how the file is structuredâwhether it has one item per line, comma-separated values or raw contentâdifferent approaches are available. Below are several methods to read a text file into a Python list using 2 min read Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is 2 min read How to Create a List of N-Lists in Python In Python, we can have a list of many different kinds, including strings, numbers, and more. Python also allows us to create a nested list, often known as a two-dimensional list, which is a list within a list. Here we will cover different approaches to creating a list of n-lists in Python. The diffe 3 min read How to Load a Massive File as small chunks in Pandas? When working with massive datasets, attempting to load an entire file at once can overwhelm system memory and cause crashes. Pandas provides an efficient way to handle large files by processing them in smaller, memory-friendly chunks using the chunksize parameter. Using chunksize parameter in read_c 5 min read How to split a Dataset into Train and Test Sets using Python One of the most important steps in preparing data for training a ML model is splitting the dataset into training and testing sets. This simply means dividing the data into two parts: one to train the machine learning model (training set), and another to evaluate how well it performs on unseen data ( 3 min read How to Take a Tuple as an Input in Python? Tuples are immutable data structures in Python making them ideal for storing fixed collections of items. In many situations, you may need to take a tuple as input from the user. Let's explore different methods to input tuples in Python.The simplest way to take a tuple as input is by using the split( 3 min read How to Get First N Items from a List in Python Accessing elements in a list has many types and variations. This article discusses ways to fetch the first N elements of the list.Using List Slicing to Get First N Items from a Python ListThis problem can be performed in 1 line rather than using a loop using the list-slicing functionality provided b 4 min read Python | Custom slicing in List Sometimes, while working with Python, we can come to a problem in which we need to perform the list slicing. There can be many variants of list slicing. One can have custom slice interval and slicing elements. Let's discuss problem to such problem. Method : Using compress() + cycle() The combination 2 min read Like