Home >Backend Development >Python Tutorial >How to Efficiently Iterate Over a List in Chunks in Python?

How to Efficiently Iterate Over a List in Chunks in Python?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-23 09:06:241052browse

How to Efficiently Iterate Over a List in Chunks in Python?

How to Iterate Over a List in Chunks

The need to iterate over a list in chunks arises in various programming scenarios, such as processing large datasets. This article delves into the most efficient and Pythonic ways to achieve this task.

Traditionally, a C-style approach using a range loop with explicit index manipulation was employed:

for i in range(0, len(ints), 4):
    # dummy op for example code
    foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]

While this method may be straightforward, it lacks Pythonic elegance.

A more Pythonic alternative was proposed:

while ints:
    foo += ints[0] * ints[1] + ints[2] * ints[3]
    ints[0:4] = []

This approach leverages Python's list slicing to remove the processed elements, presenting a cleaner solution.

Python 3.12's Introduction of chunker()

With the release of Python 3.12, the highly anticipated chunker() function was introduced:

from itertools import islice

def chunker(seq, size):
    it = iter(seq)
    while True:
        chunk = list(islice(it, size))
        if not chunk:
            break
        yield chunk

This function elegantly iterates over any type of sequence, splitting it into chunks of the desired size. The following examples illustrate its versatility:

text = "I am a very, very helpful text"

for group in chunker(text, 7):
    print(repr(group),)
# 'I am a ' 'very, v' 'ery hel' 'pful te' 'xt'

print('|'.join(chunker(text, 10)))
# I am a ver|y, very he|lpful text

animals = ['cat', 'dog', 'rabbit', 'duck', 'bird', 'cow', 'gnu', 'fish']

for group in chunker(animals, 3):
    print(group)
# ['cat', 'dog', 'rabbit']
# ['duck', 'bird', 'cow']
# ['gnu', 'fish']

The chunker() function enhances Python's already rich ecosystem of list manipulation and further exemplifies its user-friendliness.

The above is the detailed content of How to Efficiently Iterate Over a List in Chunks in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn