Home > Backend Development > Python Tutorial > How Can I Efficiently Chunk an Iterator in Python?

How Can I Efficiently Chunk an Iterator in Python?

Patricia Arquette
Release: 2024-11-29 08:51:10
Original
1039 people have browsed it

How Can I Efficiently Chunk an Iterator in Python?

Iterating Iterators in Chunks

Consider a scenario where you possess an iterator l = [1, 2, 3, 4, 5, 6, 7] and wish to partition it into chunks of size 3, resulting in an iterator containing [[1, 2, 3], [4, 5, 6], [7]].

itertools Solution

The Python standard library offers a convenient solution within the itertools module:

import itertools

def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
    """Collect data into non-overlapping fixed-length chunks or blocks."""

    args = [iter(iterable)] * n
    if incomplete == 'fill':
        return zip_longest(*args, fillvalue=fillvalue)
    elif incomplete == 'strict':
        return zip(*args, strict=True)
    elif incomplete == 'ignore':
        return zip(*args)
    else:
        raise ValueError('Expected fill, strict, or ignore')

chunks = list(grouper(l, 3))  # Output: [[1, 2, 3], [4, 5, 6], [7]]
Copy after login

Standard Library Enhanced Version

A specialized function, batched, introduced in recent versions of the itertools recipes, precisely addresses this chunking requirement:

from itertools import batched

chunks = list(batched(l, 3))  # Output: [[1, 2, 3], [4, 5, 6], [7]]
Copy after login

Non-General Solution

For sequences specifically, a simpler approach that handles last chunks effectively is:

chunks = [my_list[i:i + chunk_size] for i in range(0, len(my_list), chunk_size)]
Copy after login

The above is the detailed content of How Can I Efficiently Chunk an Iterator in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template