This article is a look at Python lists and how to split them – starting with the different implementations in the standard Python and then looking at list comprehension of Python.

The solutions described in this article will work for lists of ints, strings, lists and other objects.

Short summary of the articles examples:

python-split-list-into-evenly-sized-lists

Solution 1: Python split list with itertools

We'll begin with the itertools and show how to split a list with zip_longest.

We will declare a range in Python of 21 elements and split it into groups of 4 elements. We will convert the range to a list.

As you can see from the example below in case of a smaller group - the result list will be filled with None.

import itertools as it

my_list = list(range(21))
n = 4

list(it.zip_longest(*[iter(my_list)] * n))

result:

[(0, 1, 2, 3),
 (4, 5, 6, 7),
 (8, 9, 10, 11),
 (12, 13, 14, 15),
 (16, 17, 18, 19),
 (20, None, None, None)]

Solution 2: Python split list with list comprehension

Another alternative to split lists into equal chunks is to use Python list comprehensions.

In this case we are going to split a list of 21 elements into lists of 5 elements each.

my_list = list(range(21))
n = 5

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

result:

[[0, 1, 2, 3, 4],
 [5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14],
 [15, 16, 17, 18, 19],
 [20]]

The difference to the previous solutions is that the last list will have only elements from the initial list.

Solution 3: Python split list with numpy

My favourite solution is coming from numpy. There are two methods which can split array or list:

  • np.split
  • np.array_split

They can split list in a different way:

np.array_split defines the total number of chunks, not the number of elements per chunk.

So if we like to split 23 elements into 5 groups:

import numpy as np

my_list = list(range(23))
np.array_split(my_list, 5)

we will get 3 lists of 5 elements and 2 of 4 elements:

[array([0, 1, 2, 3, 4]),
 array([5, 6, 7, 8, 9]),
 array([10, 11, 12, 13, 14]),
 array([15, 16, 17, 18]),
 array([19, 20, 21, 22])]

np.split gets indices_or_sections. So in order to split the same list of 23 items into 5 lists we can use:

n = 5
np.split(my_list, range(n, n*n, n))

which results into:

[array([0, 1, 2, 3, 4]),
 array([5, 6, 7, 8, 9]),
 array([10, 11, 12, 13, 14]),
 array([15, 16, 17, 18, 19]),
 array([20, 21, 22])]

How does it work? We are giving the list and the indices where we would like to break the list.

So if we like to get chunks of 5 items per sublist we are going to generate indices as follow:

range(5, 25, 5)

output:

[5, 10, 15, 20]

Solution 4: Python split list with zip

One more option is to split lists in Python with zip. The solution seems to be a simple one as:

my_list = list(range(15))
n = 3

list(zip(*[iter(my_list)]*n))

which will produce:

[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]

but in case of not evenly spread elements will produce unexpected results as:

my_list = list(range(17))
n = 3

list(zip(*[iter(my_list)]*n))

result:

[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]