Splitting a List into chunks in Python

+4 votes
Suppose that I wish to chop up a list in python into equal-length pieces, is there an elegant way to do this? I don't want to try out the ordinary way of using an extra list and keeping a counter variable to cut the list at the right indices.
Apr 13, 2018 in Python by kaalabilli
• 1,090 points
35,411 views

7 answers to this question.

+2 votes
Best answer

Here's a generator that yields the chunks of a list:

Here n is the size of the chunks

The list created below contains nested lists containing the chunks of the list

def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]
print(list(chunks(range(5, 30))))


Hope this helps!!

If you need to learn more about Python, It's recommended to join Online Python Training Course today.

Thanks!

answered Apr 13, 2018 by Nietzsche's daemon
• 4,260 points

selected Oct 12, 2018 by Omkar
Can I use the same logic to chunk a dictionary into n size, Can you suggest me some logic if not and also I need to return it as JSON so it should be JSON serilizable.
+1 vote

Use numpy

>>> import numpy
>>> x = range(25)
>>> l = numpy.array_split(numpy.array(x),6)

or

>>> import numpy
>>> x = numpy.arange(25)
>>> l = numpy.array_split(x,6);

You can also use numpy.split but that one throws in error if the length is not exactly divisible.

answered Oct 12, 2018 by findingbugs
• 4,780 points
+1 vote

Here's a generator that yields the chunks you want:

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[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]]

If you're using Python 2, you should use xrange() instead of range():

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

Also you can simply use list comprehension instead of writing a function. Python 3:

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

Python 2 version:

[l[i:i + n] for i in xrange(0, len(l), n)]
answered Oct 12, 2018 by abc
+1 vote

If you want something super simple:

def chunks(l, n):
    n = max(1, n)
    return (l[i:i+n] for i in xrange(0, len(l), n))
answered Oct 12, 2018 by riya
+1 vote

Directly from the (old) Python documentation (recipes for itertools):

from itertools import izip, chain, repeat

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

The current version, as suggested by J.F.Sebastian:

#from itertools import izip_longest as zip_longest # for Python 2.x
from itertools import zip_longest # for Python 3.x
#from six.moves import zip_longest # for both (uses the six compat library)

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return zip_longest(*[iter(iterable)]*n, fillvalue=padvalue)

I guess Guido's time machine works—worked—will work—will have worked—was working again.

These solutions work because [iter(iterable)]*n (or the equivalent in the earlier version) creates one iterator, repeated n times in the list. izip_longest then effectively performs a round-robin of "each" iterator; because this is the same iterator, it is advanced by each such call, resulting in each such zip-roundrobin generating one tuple of n items.

answered Oct 12, 2018 by kalpesh
+1 vote

I know this is kind of old but I don't why nobody mentioned numpy.array_split:

lst = range(50)
In [26]: np.array_split(lst,5)
Out[26]: 
[array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
 array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
 array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29]),
 array([30, 31, 32, 33, 34, 35, 36, 37, 38, 39]),
 array([40, 41, 42, 43, 44, 45, 46, 47, 48, 49])]
answered Oct 12, 2018 by rani
0 votes

You can split a list into chunks with this code:

def chunks(l, chunk_size):
    result = []
    chunk = []
    for item in l:
        chunk.append(item)
        if len(chunk) == chunk_size:
            result.append(chunk)
            chunk = []

    # don't forget the remainder!
    if chunk:
        result.append(chunk)

    return result

print(chunks([1, 2, 3, 4, 5], 2))

Hope it helps you OP!

answered Jun 11, 2019 by Chris

Related Questions In Python

0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,465 views
0 votes
1 answer

In Python, how do I read a file line-by-line into a list?

with open(fname) as f:     content = f.readlines() # you ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 13,300 points
1,799 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
+1 vote
8 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
36,006 views
0 votes
1 answer

Adding elements to a list in python

Use extend() instead: l = [5, 7, 12, ...READ MORE

answered Apr 23, 2018 in Python by Nietzsche's daemon
• 4,260 points
814 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP