# Python Counter: count items

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-counter-full-en
`collections.Counter(iterable)` counts how often each item appears. `most_common(n)` returns the n most frequent items as pairs.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-83-A, TE-83-B and TE-83-C. The expected non-zero exit is TE-83-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-83-A: Counter counts items and most_common returns the top pair

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from collections import Counter
c = Counter('banana')
print(c['a'], c['n'], c['b'], c['z'])
print(c.most_common(1))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
3 2 1 0
[('a', 3)]
```

Exit status: 0. The output matches the expected text written before the run.

### TE-83-B: update adds counts to a Counter, and the values add up to the number of items counted

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from collections import Counter
c = Counter([1, 1, 2])
c.update([2, 2])
print(c)
print(sum(c.values()))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
Counter({2: 3, 1: 2})
5
```

Exit status: 0. This status and the output match what was written before the run.

### TE-83-C: expected non-zero exit: Counter of an integer raises TypeError because an integer is not iterable

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from collections import Counter
Counter(5)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b22-jyf561ga/work/s.py", line 2, in <module>
    Counter(5)
  File "/usr/lib/python3.10/collections/__init__.py", line 577, in __init__
    self.update(iterable, **kwds)
  File "/usr/lib/python3.10/collections/__init__.py", line 670, in update
    _count_elements(self, iterable)
TypeError: 'int' object is not iterable
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-83-B: the counts after the update are 2 for the number 1 and 3 for the number 2, and the values add up to 5. TE-83-C: Counter(5) stopped with exit status 1. The last line of standard error is TypeError: 'int' object is not iterable.

## Limits

TE-83-A counts a string and TE-83-B counts numbers. Order of equal counts was not tested.

## Source

- https://docs.python.org/3/library/collections.html#collections.Counter

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-83-A` | Counter counts items and most_common returns the top pair | 0 | PASS | `6af42e613529` |
| `TE-83-B` | update adds counts to a Counter, and the values add up to the number of items counted | 0 | PASS | `39ec32454fdf` |
| `TE-83-C` | expected non-zero exit: Counter of an integer raises TypeError because an integer is not iterable | 1 | PASS | `dc5e7aa65aa5` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python Defaultdict: a dict that fills missing keys](https://kairos-2z6s.onrender.com/res/python-defaultdict-full-en)
- [Python List Slicing: take part of a list](https://kairos-2z6s.onrender.com/res/python-list-slicing-full-en)
- [Python: count with a dict or Counter, and meet sets and tuples](https://kairos-2z6s.onrender.com/res/python-counting-sets-tuples-full-en)
- [Python comprehensions: build a list, dict or set in one line](https://kairos-2z6s.onrender.com/res/python-comprehensions-full-en)
- [Python csv module: read and write rows with quoting rules](https://kairos-2z6s.onrender.com/res/python-csv-full-en)
