Here's a generator that yields evenly-sized chunks:

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]
import pprint
pprint.pprint(list(chunks(range(10, 75), 10)))
[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74]]

For Python 2, using xrange instead of range:

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in xrange(0, len(lst), n):
        yield lst[i:i + n]

Below is a list comprehension one-liner. The method above is preferable, though, since using named functions makes code easier to understand. For Python 3:

[lst[i:i + n] for i in range(0, len(lst), n)]

For Python 2:

[lst[i:i + n] for i in xrange(0, len(lst), n)]
Answer from Ned Batchelder on Stack Overflow
🌐
Python
docs.python.org › 3 › library › chunk.html
chunk — Read IFF chunked data
This module is no longer part of the Python standard library. It was removed in Python 3.13 after being deprecated in Python 3.11. The removal was decided in PEP 594. The last version of Python tha...
🌐
GeeksforGeeks
geeksforgeeks.org › python › break-list-chunks-size-n-python
Break a List into Chunks of Size N in Python - GeeksforGeeks
October 28, 2025 - DSA Python · Data Science · NumPy ... : 28 Oct, 2025 · Given a list of elements and a number n, the task is to split the list into smaller sublists (chunks), where each sublist contains at most n elements....
Author: isaacus-dev
🌐
TutorialsPoint
tutorialspoint.com › python_text_processing › python_chunks_and_chinks.htm
Python Text Processing - Chunks and Chinks
Python TechnologiesDatabasesComputer ... Tutorials View All Categories ... Chunking is the process of grouping similar words together based on the nature of the word....
🌐
OneUptime
oneuptime.com › home › blog › how to split a list into chunks in python
How to Split a List Into Chunks in Python
January 25, 2026 - The generator approach uses constant extra memory for the chunks being produced, while the list comprehension uses memory proportional to the number of chunks. For most cases, the list comprehension is clear and sufficient. Switch to a generator when working with large datasets or when memory is constrained. Use itertools.batched if you are on Python 3.12 or later for a clean standard library solution.
🌐
YouTube
youtube.com › watch
How To Split Any List Into Equally-Sized Chunks (Python Recipes) - YouTube
In this video I'm going to be showing you how you can split any list or iterable into equally-sized chunks in Python▶ Become job-ready with Python:https://ww...
Published: July 3, 2024
Find elsewhere
🌐
Medium
medium.com › ai-does-it-better › splitting-a-list-into-evenly-sized-chunks-in-python-a993786a6b6e
Splitting a List into Evenly Sized Chunks in Python | by Doug Creates | AI Does It Better | Medium
March 19, 2024 - Dividing a list into evenly sized chunks involves calculating the size of each chunk and then iterating over the list to slice it into sublists. This transforms a single list into a list of lists, each containing a specified number of elements. # Python program to split a list into chunks of varying sizes def split_list_varying_sizes(lst, chunk_sizes): chunks = [] start = 0 for size in chunk_sizes: if start < len(lst): chunks.append(lst[start:start + size]) start += size else: break return chunks # Example usage example_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] chunk_sizes = [3, 2, 5] chunks = split_list_varying_sizes(example_list, chunk_sizes) print(chunks) # Output: [[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]]
🌐
Python
docs.python.org › 3.9 › library › chunk.html
chunk — Read IFF chunked data — Python 3.9.25 documentation
The proposed usage of the Chunk class defined here is to instantiate an instance at the start of each chunk and read from the instance until it reaches the end, after which a new instance can be instantiated.
🌐
Dermitch
dermitch.de › post › python-chunk-iterable
Python: Split up iterable into evenly-sized chunks - Mitch's Blog
As soon as the iterator has ended, whatever is left will be yielded as last chunk. You cannot use return for that, as it's would drop the value. Since the last few releases, Python added great support for adding type hints to code, which adds clarity about what functions receive and return, ...
🌐
Real Python
realpython.com › how-to-split-a-python-list-into-chunks
How to Split a Python List or Iterable Into Chunks – Real Python
July 23, 2026 - You can split a list into chunks without using a library by implementing a custom loop with slicing. You handle infinite data streams in Python by leveraging itertools.batched() for lazy evaluation.
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › chunk.html
chunk — Python Standard Library
The proposed usage of the Chunk class defined here is to instantiate an instance at the start of each chunk and read from the instance until it reaches the end, after which a new instance can be instantiated.
🌐
PyPI
pypi.org › project › chunk
chunk · PyPI
You’ll get: Chunk, chunkify, and unchunkify. ... $ python -m chunk.test $ python -m chunk.test -v | tail -n22 1 items had no tests: chunk.fromYAML 16 items passed all tests: 8 tests in chunk 13 tests in chunk.Chunk 7 tests in chunk.Chunk.__contains__ 4 tests in chunk.Chunk.__delattr__ 7 tests in chunk.Chunk.__getattr__ 3 tests in chunk.Chunk.__repr__ 5 tests in chunk.Chunk.__setattr__ 2 tests in chunk.Chunk.fromDict 2 tests in chunk.Chunk.toDict 5 tests in chunk.chunkify 2 tests in chunk.from_yaml 3 tests in chunk.toJSON 6 tests in chunk.toYAML 3 tests in chunk.to_yaml 3 tests in chunk.to_yaml_safe 4 tests in chunk.unchunkify 77 tests in 17 items.
      » pip install chunk
    
Published: Jan 01, 2014
Version: 2.0.0
🌐
Programiz
programiz.com › python-programming › examples › list-chunks
Python Program to Split a List Into Evenly Sized Chunks
To understand this example, you should have the knowledge of the following Python programming topics: ... def split(list_a, chunk_size): for i in range(0, len(list_a), chunk_size): yield list_a[i:i + chunk_size] chunk_size = 2 my_list = [1,2,3,4,5,6,7,8,9] print(list(split(my_list, chunk_size)))
🌐
Python Engineer
python-engineer.com › posts › split_list_in_chunks
How to split a List into equally sized chunks in Python - Python Engineer
from itertools import islice def chunk(lst, n): it = iter(lst) return iter(lambda: tuple(islice(it, n)), ()) chunks = list(chunk(my_list, 3)) # [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)] In Python 3.12, you can use the new itertools.batched method, which was implemented exactly for this purpose:
🌐
Vultr
docs.vultr.com › python › examples › split-a-list-into-evenly-sized-chunks
Python Program to Split a List Into Evenly Sized Chunks | Vultr Docs
April 10, 2025 - This function iterates over the original list and creates a new chunk on each iteration by slicing the list from the current index i to i + chunk_size. The slices are then appended to the chunks list. In the example provided, the Python list is split into chunks where each chunk (except possibly the last) has 4 elements.
🌐
Reddit
reddit.com › r/learnpython › chunking in python---how to set the "chunk size" of read lines from file read with python open()?
r/learnpython on Reddit: Chunking in Python---How to set the "chunk size" of read lines from file read with Python open()?
September 15, 2016 -

I have a fairly large text file which I would like to run in chunks. In order to do this with the subprocess library, one would execute following shell command:

"cat hugefile.log"

with the code:

import subprocess
task = subprocess.Popen("cat hugefile.log", shell=True,  stdout=subprocess.PIPE)
data = task.stdout.read()

Using print(data) will spit out the entire contents of the file at once. How can I present the number of chunks, and then access the contents of this file by the chunk size (e.g. chunk = three lines at a time).

It must be something like:

chunksize = 1000   # break up hugefile.log into 1000 chunks

for chunk in data:
    print(chunk)

The equivalent question with Python open() of course uses the code

with open('hugefile.log', 'r') as f:
     read_data = f.read()

How would you read_data in chunks?

🌐
30 Seconds of Code
30secondsofcode.org › home › python › split list into chunks
Split a Python list into chunks - 30 seconds of code
July 17, 2024 - Chunks a list into a set amount of smaller lists or into lists of a specified size.
🌐
Medium
medium.com › @gunkurnia › supercharge-your-python-master-memory-efficient-data-processing-with-chunking-82e3793cd178
Supercharge Your Python: Master Memory-Efficient Data Processing with Chunking | by GunKurnia | Medium
November 10, 2024 - “Chunk processing in Python optimizes performance by handling data in manageable segments, improving efficiency and memory usage for large…