How to split a Python list into evenly sized chunks Last Updated : 04 Jun, 2024 Summarize Comments Improve Suggest changes Share 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 Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like