Skip to content

reuse-of-groupby-generator (B031)

Added in v0.0.260 · Related issues · View source

Derived from the flake8-bugbear linter.

What it does

Checks for multiple usage of the generator returned from itertools.groupby().

Why is this bad?

Using the generator more than once will do nothing on the second usage. If that data is needed later, it should be stored as a list.

Example:

import itertools

for name, group in itertools.groupby(data):
    for _ in range(5):
        do_something_with_the_group(group)

Use instead:

import itertools

for name, group in itertools.groupby(data):
    values = list(group)
    for _ in range(5):
        do_something_with_the_group(values)