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 - List comprehension is an efficient method for chunking a list into smaller sublists.
๐ŸŒ
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 itertools.islice function works with any iterable, not just lists. from itertools import islice def chunk_iterable(iterable, chunk_size): """Split any iterable into chunks. Works with iterators that do not support indexing.
๐ŸŒ
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.
๐ŸŒ
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!

๐ŸŒ
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 - Deque supports fast popping, enabling an efficient way to chunk lists. ... from collections import deque def deque_chunker(data, size): dq = deque(data) while dq: yield [dq.popleft() for _ in range(min(size, len(dq)))] words = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink'] chunk_size = 3 print(list(deque_chunker(words, chunk_size)))
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ splitting-python-lists-into-equal-sized-chunks-a174907ed413
Splitting Python Lists into Equal-Sized Chunks
August 24, 2024 - The Python scripts provided earlier serve as practical solutions for dividing lists and strings into equal-sized chunks, a frequent requirement in data processing tasks. The first script, aimed at list segmentation, introduces a function named chunk_list which accepts two parameters: the list to be divided and the desired chunk size.
๐ŸŒ
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 - It's a straightforward demonstration of how to divide a list into evenly sized chunks. ... # Python program to split a list into chunks of size n using list comprehension # Function to split list def split_list(lst, n): # Using list comprehension to split list return [lst[i:i + n] for i in range(0, len(lst), n)] # Example list example_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Desired chunk size chunk_size = 3 # Splitting the list chunks = split_list(example_list, chunk_size) # Printing the chunks print(chunks) # Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # This demonstrates how a list can be divided into smaller chunks of a specified size.
๐ŸŒ
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 list", one of the first results is an already answered question on StackOverflow.
๐ŸŒ
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 - In order to chunk a list into n smaller lists, you first need to calculate the size of each chunk, using math.ceil() and len().
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ how-to-split-a-list-into-even-chunks-in-python
How to Split a List into Even Chunks in Python
September 19, 2021 - In this article, we show how to split a list into even sized chunks in Python - splitting into chunks of N elements and into N chunks of equal size.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python split a list into evenly sized chunks?
Python Split a list into evenly sized chunks? - Spark By {Examples}
May 31, 2024 - How to split a list into evenly-sized elements in Python? To split the list evenly use methods like slicing, zip(), iter(), numpy.array_split(), list
๐ŸŒ
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.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-split-list
How to Split Lists in Python: Basic and Advanced Methods | DataCamp
June 21, 2024 - The numpy library in Python is useful for splitting arrays to sublists. The .array_split() function allows splits when specifying the number of splits. # Import the numpy library and alias it as np import numpy as np # Define a list of integers from 1 to 15 my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] # Use numpy's array_split function to split the list into 3 chunks chunks = np.array_split(my_list, 3) # Convert each chunk back to a regular list and print the resulting list of chunks print([list(chunk) for chunk in chunks]) # Expected output: # [[np.int32(1), np.int32(2), np.int32(3), np.int32(4), np.int32(5)], [np.int32(6), np.int32(7), np.int32(8), np.int32(9), np.int32(10)], [np.int32(11), np.int32(12), np.int32(13), np.int32(14), np.int32(15)]]
๐ŸŒ
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)))
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-do-you-split-a-list-into-evenly-sized-chunks-in-python
How do you split a list into evenly sized chunks in Python?
March 24, 2026 - It offers a multidimensional array object with outstanding speed as well as tools for interacting with these arrays. We have a array_split() method of NumPy which divides a list into chunks.
๐ŸŒ
Tech Spy
tech-spy.co.uk โ€บ home โ€บ development โ€บ python โ€บ how to split a list into chunks in python
How To Split A List Into Chunks In Python - Technology Spy
March 17, 2017 - The first is a list of data and the second is the number of chunks you require. #!/usr/bin/env python def main(): # Create an example list full of numbers MyList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] # Print the result of passing the list to the # function get_chunks with a value of 4 print get_chunks(MyList,4) def get_chunks(MyList, n): # Declare some empty lists chunk = [] chunks = [] # Step through the data n elements # at a time for x in range(0, len(MyList), n): # Extract n elements chunk = MyList[x:x+n] # Add them to list chunks.append(chunk) # Return the new list return chunks # Run main function main()
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-efficiently-split-a-python-list-into-n-chunks-397986
How to efficiently split a Python list into N chunks | LabEx
Each of these approaches has its own advantages and use cases, which we will explore in the next section. One of the simplest ways to split a list in Python is to use list slicing.
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ how to split a python list into evenly sized chunks?
How to Split a Python List into Evenly Sized Chunks? - Intellipaat
February 3, 2026 - Explanation: In this code, the for loop is used for slicing the given list into an equal number of chunks each chunk is stored one at a time. The list comprehension method using range() in Python allows splitting a list into chunks.