# Python: count with a dict or Counter, and meet sets and tuples

Published: 2026-11-30
Updated: 2026-12-01

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-counting-sets-tuples-full-en
A dictionary or Counter counts occurrences; a set removes duplicates; a tuple is an immutable sequence. Set display order is not fixed.

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-46-A, TE-46-B and TE-46-C. The expected non-zero exit is TE-46-C (exit status 1). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-46-A: set removes duplicates but does not promise display order; Counter counts occurrences

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
python3 -c "from collections import Counter; print(sorted(set(['b','a','b']))); c=Counter('abracadabra'); print(c['a'], len(c)); print(sorted(c.items()))"
```

Recorded output:

```
['a', 'b']
5 5
[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
```

### TE-46-B: a Counter returns 0 for a missing key and does not add that key

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

```bash
cat > s.py <<'EOS'
from collections import Counter
c = Counter('aab')
print(c['z'], 'z' in c, len(c))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
0 False 2
```

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

### TE-46-C: expected non-zero exit: a list cannot go into a set because it is not hashable

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

```bash
cat > s.py <<'EOS'
print(set([[1]]))
EOS
```

The file `../err.txt` sits one level above the working folder, inside the same throwaway tree. The output block below shows only the last line of the error; the full traceback went to `../err.txt`.

Command:

```bash
python3 s.py 2>../err.txt; rc=$?; tail -n 1 ../err.txt; exit $rc
```

Recorded output:

```
TypeError: unhashable type: 'list'
```

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

## Reading the recorded output

The first line is sorted(set(['b','a','b'])), which gave ['a', 'b']: the set kept one b, and sorted put the items in order. The second line holds two numbers for Counter('abracadabra'). c['a'] is 5, the number of a letters in the text, and len(c) is 5, the number of different letters. The last line lists the pairs in sorted order: a 5, b 2, c 1, d 1, r 2. Those counts add up to 11, which is the length of the word abracadabra.

TE-46-B: for Counter('aab'), c['z'] gave 0, 'z' in c gave False, and len(c) stayed 2, so reading a missing key returned 0 and did not add it. TE-46-C: putting a list into a set stopped with TypeError: unhashable type: 'list', with exit status 1.

## Limits

Set display order is not fixed. The counts come from the input shown.

## Source

- https://docs.python.org/3/tutorial/datastructures.html#sets
- https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
- https://docs.python.org/3/library/collections.html#collections.Counter

## Evidence

- `TE-46-A`: set removes duplicates but does not promise display order; Counter counts occurrences
- `TE-46-B`: a Counter returns 0 for a missing key and does not add that key
- `TE-46-C`: expected non-zero exit: a list cannot go into a set because it is not hashable

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-46-A` | set removes duplicates but does not promise display order; Counter counts occurrences | 0 | PASS | `b737414675b1` |
| `TE-46-B` | a Counter returns 0 for a missing key and does not add that key | 0 | PASS | `9b40b550abff` |
| `TE-46-C` | expected non-zero exit: a list cannot go into a set because it is not hashable | 1 | PASS | `cedb13bf6d8f` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python modules and imports](https://kairos-2z6s.onrender.com/res/python-modules-imports-full-en)
- [Python sorting: sorted, sort and the key argument](https://kairos-2z6s.onrender.com/res/python-sorting-full-en)
- [Python csv module: read and write rows with quoting rules](https://kairos-2z6s.onrender.com/res/python-csv-full-en)
- [Python Counter: count items](https://kairos-2z6s.onrender.com/res/python-counter-full-en)
- [Python Dataclasses: a class that stores values](https://kairos-2z6s.onrender.com/res/python-dataclasses-full-en)
