python/counting-loop-without-counter
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| Python | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
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 countsfrom collections import Counter
def word_counts(words: list[str]) -> Counter[str]: return Counter(words)What the rule reports
Section titled “What the rule reports”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.
How to fix it
Section titled “How to fix it”With --unsafe-fixes, xpd replaces the loop with Counter when the previous statement initializes the counts. Review the change if later code uses the loop variable or relies on plain dict behavior.
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 +.
Diagnostic message
Section titled “Diagnostic message”This loop only counts the items of $ITEMS. Write $D = collections.Counter(iter($ITEMS)) in place of the loop.See all rules in the Python pack.