Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

How to split a Python list into evenly sized chunks

abhilash

Overview

Given a list of elements, learn to return sublists/chunks of the given size.

Example 1

Input

lst = [1, 2, 3, 4, 5, 6]
chk_size = 3

Output

[[1,2,3], [4,5,6]]

Example 2:

Input

lst = [1, 2, 3, 4, 5]
chk_size = 3

Output

[[1,2,3], [4,5]]

The yield keyword

The yield keyword is used to produce a series of values over time.

Refer to Using the keyword Yield in Python to learn more.

In the code below, we iterate over the given list by jumping the list by chunk size in every iteration. In every iteration, we yield the sublist from the index we’re iterating to the index at the starting of the next chunk.

The chunks method returns an iterator. We can either convert the iterator to a list by passing the iterator to list() or by iterating it until all the elements in the iterator get exhausted.

Example


def chunks(lst, chk_size):
    for index in range(0, len(lst), chk_size):
        yield lst[index:index + chk_size]

chk_size = 3

lst = list(range(15))

chunked_lst = list(chunks(lst, chk_size))

print("Original List - ", lst)
print("Chunked List of chunk size %s - %s" % (chk_size, chunked_lst))

Explanation

  • Lines 2–5: We define a function called chunks that accepts a list and chunk size and returns a chunk every time it’s called.
  • Line 6: We define the chunk size, chk_size.
  • Line 8: We define a Python list, lst.
  • Line 10: We use the chunks() method to chunk lst into chunks of size, chk_size.
  • Lines 12-13: The original and the chunked list are printed.

Using list comprehension

List comprehensions are a concise way to create lists. They’re used to create a new list by iterating over another list.

Refer to What is list comprehension in Python? to learn more.

In the code below, we use list comprehension to return sublists/chunks of the given size. Here, we iterate over the given list by jumping the list by chunk size in every iteration. In every iteration, we get the sublist from the index we’re iterating to the index at the start of the next chunk.

lst = list(range(5))
chk_size = 4
chunks = [lst[i:i + chk_size] for i in range(0, len(lst), chk_size)]

print("Original List - ", lst)
print("Chunked List of chunk size %s - %s" % (chk_size, chunks))

Explanation

  • Line 1: We define a Python list, lst.
  • Line 2: We define the chunk size, chk_size.
  • 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.

Using itertools

We can chunk the list into the given sizes using the islice method of the itertools module.

Refer to What is itertools.islice() method in Python? to learn more.

We create an iterator from the given list using the iter() function. Then we use islice method to chunk the iterator. Using the iter() method with a lambda and the sentinel value as an empty tuple we return an iterator of chunks.

Refer to How to use iter() in Python to learn more.

from itertools import islice

def chunks(lst, chk_size):
    lst_it = iter(lst)
    return iter(lambda: tuple(islice(lst_it, chk_size)), ())

lst = list(range(5))
chk_size = 4
chunked_list = list(chunks(lst, chk_size))

print("Original List - ", lst)
print("Chunked List of chunk size %s - %s" % (chk_size, chunked_list))

RELATED TAGS

python
RELATED COURSES

View all Courses

Keep Exploring