Skip to content

python/counting-loop-without-counter

Rule counting-loop-without-counter
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

A loop whose only job is to count items with a dict does what collections.Counter does in one call. Write counts = Counter(items).

def word_counts(words: list[str]) -> dict[str, int]:
counts: dict[str, int] = {}
for word in words:
counts[word] = counts.get(word, 0) + 1
return counts
from collections import Counter
def word_counts(words: list[str]) -> Counter[str]:
return Counter(words)

The rule reports a loop whose only statement is counts[item] = counts.get(item, 0) + 1, where item is the loop variable and the same function starts counts as {} or dict(). It leaves alone a loop that holds another statement, a loop that counts a part of each item, and a dict that the function takes as a parameter.

xpd has no automatic fix, so make the change by hand.

The Python documentation describes a Counter as "a dict subclass for counting hashable objects". On Python 3.15.2 on one machine, counting 100,000 items under 50 keys took 2.59 milliseconds with the loop and 1.79 with Counter(words). The rule stops at that one shape because the other two were not faster. A Counter filled with counts[word] += 1 in a loop took 4.43 milliseconds, and Counter(item.kind for item in items) took 4.12 against 4.23 for the loop.

A Counter returns 0 for a key that is missing and doesn't raise a KeyError. It also has most_common(3) for the largest counts, and two of them add with +.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This loop only counts the items of $ITEMS. Write $D = collections.Counter($ITEMS) in place of the loop.

Browse the other rules of the Python pack.