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 12,911 views

## 7 answers to this question.

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))))
```
• 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.

• 4,770 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

"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"

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)

"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"

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

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

## 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

## Sorting a list of strings in Python

Use the sort function mylist.sort() READ MORE

## Storing a list of arrays into a CSV file and retrieving it back in a different program

This is the code that I am ...READ MORE

+1 vote

## how can i count the items in a list?

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

## how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE

## Lowercase in Python

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

+1 vote