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 - For very large lists, itertools.islice can be a memory-efficient way to create chunks without loading the entire list into memory.
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 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 a list in sublists by values
from bisect import bisect_left def split_list(iterable, splitters): left, right = sorted(splitters) left_idx = bisect_left(iterable,left) right_idx = bisect_left(iterable,right) return [ iterable[:left_idx], iterable[left_idx:right_idx], iterable[right_idx:]] As a bonus this works even if the splitter is not in the list. More on reddit.com
๐ŸŒ r/learnpython
8
3
November 12, 2015
Efficiently divide numpy array into blocks sized Y x Y and apply function to each block

Easy way to represent your matrix in blocks (keeping intermediate steps, but you don't have to):

block_m = 8  # rows in block
block_n = 8  # cols in block
A = np.arange(256*256).reshape(256,256)  # sample data
B = A.reshape((-1, block_m, A.shape[1]//block_n, block_n))  
C = B.transpose((0,2,1,3))

This yields (in this case) a 32-by-32-by-8-by-8 array where the first two axes refer to the block matrix. i.e. C[0,0] is the 8-by-8 sub-array in the top left of your original array. In most cases, I believe C should be a view on the original array; changing C[0,0,0,0], for instance, changes A[0,0].

You can loop over the first two axes or apply vectorized functions as needed. As an example, here's me averaging the 8-by-8 subarrays:

block_means = np.sum(C, axis=(2,3)) / 64
More on reddit.com
๐ŸŒ r/learnpython
3
4
May 23, 2021
๐ŸŒ
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!

๐ŸŒ
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 - Python 3.12 introduced itertools.batched specifically for this purpose. from itertools import batched data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Returns tuples, not lists for chunk in batched(data, 3): print(chunk) # (1, 2, 3) # (4, 5, 6) # (7, 8, 9) # (10,) # Convert to lists if needed chunks = [list(chunk) for chunk in batched(data, 3)] For numeric data, NumPy's array_split handles uneven divisions cleanly.
๐ŸŒ
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 - This method compresses the logic into a single-line statement. ... data_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] chunk_size = 4 chunks = [data_list[i:i + chunk_size] for i in range(0, len(data_list), chunk_size)] print(chunks) ... Leverage NumPy for efficient chunking of numeric arrays or ranges. ... import numpy as np numbers = list(range(15)) # Creating a list from 0 to 14 chunked_array = np.array_split(numbers, 5) print([list(arr) for arr in chunked_array]) # Convert back to list
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ numpy โ€บ numpy split: split a numpy array into chunks
NumPy split: Split a NumPy Array into Chunks โ€ข datagy
May 20, 2023 - ... The NumPy split() function takes three parameters, two of which are required. By default, you need to pass in the array that you want to split, as well as the indices or number of sections you want to split.
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python split list into chunks
How to Split List Into Chunks in Python | Delft Stack
February 2, 2024 - The NumPy library can also be used to divide the list into N-sized chunks. The array_split() function divides the array into sub-arrays of specific size n.
๐ŸŒ
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.
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python lists โ€บ python: split a list (in half, in chunks)
Python: Split a List (In Half, in Chunks) โ€ข datagy
December 30, 2022 - This function allows you to split an array into a set number of arrays. Letโ€™s see how we can use NumPy to split our list into 3 separate chunks: # Split a Python List into Chunks using numpy import numpy as np a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] our_array = np.array(a_list) chunked_arrays = np.array_split(our_array, 3) chunked_list = [list(array) for array in chunked_arrays] print(chunked_list) # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
๐ŸŒ
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 - Similarly, you can chunk a list into smaller lists of a specified size. You can use the same approach as above, but instead of calculating the size of each chunk, you can use the specified size directly. 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]] ... An article collection of list helpers and tips for Python 3.6.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-split-list
How to Split Lists in Python: Basic and Advanced Methods | DataCamp
June 21, 2024 - Python list comprehension allows one to split list into chunks based on the values of the existing list.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ list-chunks
Python Program to Split a List Into Evenly Sized Chunks
chunk_size = 2 list_chunked = ... numpy as np my_list = [1,2,3,4,5,6,7,8,9] print(np.array_split(my_list, 5)) ... array_split() is a numpy method that splits a list into equal sized chunks....
๐ŸŒ
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 method demonstrates how to split a list into equal parts in Python using a straightforward loop-based technique. Define your list and the size of each chunk.
๐ŸŒ
ItsMyCode
itsmycode.com โ€บ python-split-list-into-chunks
Python Split list into chunks - ItsMyCode
October 15, 2024 - # Split a Python List into Chunks using numpy import numpy as np # define array and chunk_szie sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] our_array = np.array(sample_list) chunk_size = 2 # split the array into chunks chunked_arrays = np.array_split(our_array, len(sample_list) / chunk_size) print(chunked_arrays) # Covert chunked array into list chunked_list = [list(array) for array in chunked_arrays] print(chunked_list)
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ how to split a python list or iterable into chunks โ€“ real python
r/Python on Reddit: How to Split a Python List or Iterable Into Chunks โ€“ Real Python
February 9, 2023 - You'll also split multidimensional data to synthesize an image with parallel processing. Archived post. New comments cannot be posted and votes cannot be cast. Share ... 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')
๐ŸŒ
YouTube
youtube.com โ€บ watch
Coding Interview Challenge: Split Array into Chunks [Python] - YouTube
In this video series we will be showing you how to write up the algorithm that can split an array into chunks. This is a coding interview question that comes...
Published: September 1, 2023
๐ŸŒ
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
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ break-a-list-into-chunks-of-size-n-in-python
How do you split a list into evenly sized chunks in Python?
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.
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ python how to split a list to n chunks of even size
Python How to Split a List to N Chunks of Even Size
December 7, 2022 - To split a Python list into N equally sized chunks, determine the number of chunks and use a for loop to fill the chunks up.