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 Top answer 1 of 16
4507
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)]
2 of 16
671
Something super simple:
def chunks(xs, n):
n = max(1, n)
return (xs[i:i+n] for i in range(0, len(xs), n))
For Python 2, use xrange() instead of range().
Split list into smaller lists
There are likely some helper functions that make this possible in a line or two but it's almost 4am here so I'm drawing a blank. Doing it with a dumb loop is pretty simple though: def splitList(input, delim): # We'll return a list of lists outLists = [] # Read each item, if it's not the delimiter (e.g. "x") # add it to the current list. If it is the delimiter, # add the current list to the output and empty it. currentList = [] for item in input: if item == delim: if len(currentList) > 0: outLists.append(currentList) currentList = [] else: currentList.append(item) # If we get to the end and there are leftovers in the current # list, add them as well if len(currentList) > 0: outLists.append(currentList) return outLists You could call this like: splitList(your_list_above, "x") Might be some edge cases I'm not thinking of, maybe multiple delimiters in a row or something (?), didn't really test this but it should get you started Edit: Made a stupid mistake above but not going to fix it, should never use "input" as a variable, so call it something else, like "inputList" instead More on reddit.com
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
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 - from itertools import islice def chunk_iterable(iterable, chunk_size): """Split any iterable into chunks. Works with iterators that do not support indexing. """ iterator = iter(iterable) while True: chunk = list(islice(iterator, chunk_size)) if not chunk: break yield chunk # Works with generators and iterators def number_generator(): for i in range(10): yield i for chunk in chunk_iterable(number_generator(), 3): print(chunk) # [0, 1, 2] # [3, 4, 5] # [6, 7, 8] # [9] Python 3.12 introduced itertools.batched specifically for this purpose.
w3resource
w3resource.com โบ python-exercises โบ itertools โบ python-itertools-exercise-40.php
Python: Split a given list into specified sized chunks using itertools module - w3resource
July 12, 2025 - from itertools import islice def split_list(lst, n): lst = iter(lst) result = iter(lambda: tuple(islice(lst, n)), ()) return list(result) nums = [12,45,23,67,78,90,45,32,100,76,38,62,73,29,83] print("Original list:") print(nums) n = 3 print("\nSplit the said list into equal size",n) print(split_list(nums,n)) n = 4 print("\nSplit the said list into equal size",n) print(split_list(nums,n)) n = 5 print("\nSplit the said list into equal size",n) print(split_list(nums,n))
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 - This tutorial provides an overview of how to split a Python list into chunks. You'll learn several ways of breaking a list into smaller pieces using the standard library, third-party libraries, and custom code. 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 :
Educative
educative.io โบ answers โบ how-to-split-a-python-list-into-evenly-sized-chunks
How to split a Python list into evenly sized chunks
Line 3: We use list comprehension to chunk lst into chunks of size, chk_size. Lines 5โ6: We print the original and the chunked list. We can chunk the list into the given sizes using the islice method of the itertools module.
i2tutorials
i2tutorials.com โบ home โบ blogs โบ splitting a list into equal chunks in python
Splitting a List into equal Chunks in Python | i2tutorials
January 2, 2021 - #splitting list into even chunks using lambda and islice my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'] from itertools import islice def group_elements(it, size): it = iter(it) return iter(lambda: tuple(islice(it, size)), ()) for i in group_elements( my_list , 3): print(i)
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 - The list has been split into equal chunks of 7 elements each. Python has utilities to simplify this process. We can use the zip_longest function from itertools to simplify the previous function.
Sopython
sopython.com โบ canon โบ 14 โบ splitting-a-list-into-even-chunks
Splitting a list into even chunks. - sopython
chunk ยท split ยท list ยท Using the grouper recipe from the itertools documentation: >>> from itertools import zip_longest # Use izip_longest for Python 2.x >>> def grouper(iterable, n, fillvalue=None): args = [iter(iterable)] * n return zip_longest(*args, fillvalue=fillvalue) >>> list(grouper(a, 3)) [(1, 2, 3), (4, 5, 6), (7, None, None)] Another approach to exclude worrying about fill values (although itโs slightly less efficient) is to build a list of the certain chunk size and yield, eg: from itertools import islice def grouper(iterable, n): yield from iter(lambda it=iter(iterable): list(islice(it, n)), []) # Or in Python 2.7, where `yield from` doesn't exist yet, use: # for chunk in iter(lambda it=iter(iterable): list(islice(it, n)), []): # yield chunk ยท