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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ break-list-chunks-size-n-python
Break a List into Chunks of Size N in Python - GeeksforGeeks
October 28, 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. This helps in processing large data in smaller parts or batches. ... Letโ€™s explore different methods one by one. List comprehension is an efficient method for chunking a list into smaller sublists. This method creates a list of lists, where each inner list represents a chunk of the original list of a given size.
Discussions

How do I split a list of numbers into equal size (of an array?) to pass into an API as an input?
Someone in the original thread posted a great solution using range but here it is all together. abc_contents = [] with open('abc.txt', 'r') as file: abc_contents = file.readlines() # removing white space abc_contents = [i.strip() for i in abc_contents] groups = [] width = 3 for i in range(0, len(abc_contents), width): groups.append(abc_contents[i:i + width]) print(groups) More on reddit.com
๐ŸŒ r/learnpython
15
3
January 28, 2025
How to split an array into chunks of a given length in python? - Stack Overflow
Because you did not google? It is the first result for split an array into chunks of a given length in python stackoverflow. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to Split a Python List or Iterable Into Chunks โ€“ Real Python
While you have nothing in the standardlib, you have an example function for this in the itertools documentation : def grouper(iterable, n, *, incomplete='fill', fillvalue=None): "Collect data into non-overlapping fixed-length chunks or blocks" # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF args = [iter(iterable)] * n if incomplete == 'fill': return zip_longest(*args, fillvalue=fillvalue) if incomplete == 'strict': return zip(*args, strict=True) if incomplete == 'ignore': return zip(*args) else: raise ValueError('Expected fill, strict, or ignore') I'd use that. More on reddit.com
๐ŸŒ r/Python
3
54
February 9, 2023
Splitting dict into n equal chunks?
Even though this might be better solves using a non-python solution as u/K900_ suggested, I'm still interested in OP's question: how do you split a dict like that? More on reddit.com
๐ŸŒ r/learnpython
9
5
January 17, 2018
๐ŸŒ
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 - Args: lst: The list to split n: Number of parts to create Returns: List of n lists """ k, m = divmod(len(lst), n) # First m chunks get k+1 items, rest get k items return [lst[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(n)] data = list(range(10)) parts = split_into_n_parts(data, 3) print(parts) # [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]] # Note: chunks differ by at most 1 in length ยท def chunk_list(lst, chunk_size): if not lst: return [] return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)] empty = [] chunks = chunk_list(empty, 3) print(chunks) # []
๐ŸŒ
DEV Community
dev.to โ€บ askyt โ€บ break-a-list-into-chunks-of-size-n-in-python-1bj0
Break a list into chunks of size N in Python - DEV Community
January 4, 2025 - def chunk_with_yield(data, size): for start in range(0, len(data), size): yield data[start:start + size] data_list = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta'] chunk_size = 3 print(list(chunk_with_yield(data_list, ...
๐ŸŒ
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.
๐ŸŒ
Real Python
realpython.com โ€บ how-to-split-a-python-list-into-chunks
How to Split a Python List or Iterable Into Chunks โ€“ Real Python
January 27, 2025 - In this case, you follow a common idiom in Python for grouping elements of an iterable by zipping the corresponding iterator object with itself. Notice that, on line 8, you provide references pointing to exactly one copy of the iterator, which are then unpacked and passed to zip_longest(). To understand this better, you can assume a fixed chunk sizeโ€”for example, always consisting of two elementsโ€”and then rewrite the code as follows: ... # ... def split_into_pairs(iterable, fill_value=None): iterator = iter(iterable) return zip_longest( iterator, iterator, fillvalue=fill_value )
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i split a list of numbers into equal size (of an array?) to pass into an api as an input?
r/learnpython on Reddit: How do I split a list of numbers into equal size (of an array?) to pass into an API as an input?
January 28, 2025 -

I have a file abc.txt:

1234

2678

3345

4987

5765

6864

7479

I want to split the list into equal chunks (into an array?) so that it looks like this:

group 1: '1234','2678','3345'

group 2: '4987','5765','6864'

group 3: '7479'

the goal is to pass those IDs into an API. I can loop through each one of them single-y but one API call can take 30 values at a time, so it would be more efficient to send chunks of 30. (I have that part of the code working!)

my Python version is 3.6 (I know) , so cant use modules like itertools, etc. I need something easy using simple for, while loops if we can.

this is a follow-up to my original post here.

can anyone help or point me to the right direction. thank you!

Find elsewhere
๐ŸŒ
Dermitch
dermitch.de โ€บ post โ€บ python-chunk-iterable
Python: Split up iterable into evenly-sized chunks - Mitch's Blog
In a recent tool I developed, I had the need to split up a list (or any iterable) into equal-sized lists of values. In this post, I document the process on how I reached my goal, what steps it took and the decisions behind them. Since Python 3.12, you can use itertools.batched instead ๐Ÿฅณ ยท The most comfortable way to solve problems is often to just search on the internet. When looking for "python chunk ...
๐ŸŒ
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 - from math import ceil def chunk_into_n(lst, n): size = ceil(len(lst) / n) return list( map(lambda x: lst[x * size:x * size + size], list(range(n))) ) chunk_into_n([1, 2, 3, 4, 5, 6, 7], 4) # [[1, 2], [3, 4], [5, 6], [7]] Similarly, you can chunk ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ itertools.html
itertools โ€” Functions creating iterators for efficient looping
Loops over the input iterable and accumulates data into tuples up to size n. The input is consumed lazily, just enough to fill a batch. The result is yielded as soon as the batch is full or when the input iterable is exhausted: >>> flattened_data ...
๐ŸŒ
101workbook
datascience.101workbook.org โ€บ 07-wrangling โ€บ 03-data-wrangling-apps โ€บ 04-split-data-py
Split data or create data chunks - Data Science Workbook
March 13, 2026 - Use this example to split data into slices of N rows. For each data chunk a separate file will be created and all files will be saved in the CHUNKS directory on the current path. You can use Bash scripting to split small data, however, for large datasets (GBs of text file size) this python ...
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ list โ€บ python-data-type-list-exercise-233.php
Python: Chunk a given list into n smaller lists - w3resource
Use list() and range() to create a new list of size n. Use map() to map each element of the new list to a chunk the length of size. If the original list can't be split evenly, the final chunk will contain the remaining elements.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-split-list
How to Split Lists in Python: Basic and Advanced Methods | DataCamp
June 21, 2024 - The following Python function also allows one to split a list into chunks. These chunks could be custom depending on the number a user requires. # Define a function to split a list into chunks of a specified size def split_into_chunks(lst, chunk_size): chunks = [] # Initialize an empty list to store chunks # Iterate over the list with a step of chunk_size for i in range(0, len(lst), chunk_size): # Slice the list from index 'i' to 'i + chunk_size' and append it to chunks chunks.append(lst[i:i + chunk_size]) return chunks # Return the list of chunks # Define a list of integers from 1 to 16 my_li
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ split-list-into-smaller-chunks-13601
Python - Split List Into Smaller Chunks
If the length of lst is not evenly divisible by size, the last list in the returned list should contain the remaining elements. from math import ceil def chunk(lst, size): return list( map(lambda x: lst[x * size:x * size + size], list(range(ceil(len(lst) / size))))) chunk([1, 2, 3, 4, 5], 2) ## [[1, 2], [3, 4], [5]] In this challenge, you have learned how to split a list into smaller lists of a specified size.
๐ŸŒ
Newtum
blog.newtum.com โ€บ split-a-list-into-evenly-sized-chunks-in-python-using-yield
Split a List Into Evenly Sized Chunks in Python Using Yield - Newtum
April 24, 2024 - We define a function divide_chunks(l, n) that takes two arguments: the list l to be split and the chunk size n. Within the function, use a for loop to iterate over the range of indices from 0 to the length of the list l, with a step size of ...
๐ŸŒ
Sada Tech
tech.sadaalomma.com โ€บ python โ€บ python-split-list-into-chunks-of-size-n
Python Split List into Chunks of Size N - SADA Tech
February 14, 2024 - In this article, we explored different methods to split a list into chunks of size N using Python. We discussed three approaches: using list comprehension, using iterators, and using the numpy.array_split function.
๐ŸŒ
GitHub
gist.github.com โ€บ 5571582
Partition a list into N chunks of nearly equal size. ยท GitHub
def check(L, n, verbose=True): chunks = chunk(L, n) size = len(L) assert len(chunks) == n assert size == sum(len(chunk) for chunk in chunks) if verbose: msg = "\nYou want a list with {} items split into {} chunks" print msg.format(size, n) print "{} chunks produced:".format(len(chunks)) for i, x in enumerate(chunks): print "\tchunk {}, size {}".format(i+1, len(x))
๐ŸŒ
thisPointer
thispointer.com โ€บ home โ€บ list โ€บ split list into chunks of size n in python
Split List into chunks of size N in Python - thisPointer
April 30, 2023 - 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 ...
๐ŸŒ
ItsMyCode
itsmycode.com โ€บ python-split-list-into-chunks
Python Split list into chunks - ItsMyCode
October 15, 2024 - 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 ...