# Python dictionaries: lookup, default values and the KeyError

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-dict-basics-full-en
A dictionary maps keys to values; `d[key]` raises KeyError when the key is missing, while `get` returns a default instead.

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-29-A, TE-29-B and TE-29-C. The expected non-zero exit is TE-29-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-29-A: a missing key raises KeyError; get returns a default instead

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

```bash
printf 'd = {"a": 1}\nprint(d["b"])\n' > s.py
```

Command:

```bash
python3 s.py 2>&1 | tail -1; echo exit=${PIPESTATUS[0]}; python3 -c "d={'a':1}; print(d.get('b','missing'))"
```

Recorded output:

```
KeyError: 'b'
exit=1
missing
```

This run checks the exit status and records the output; it asserts no expected text.

### TE-29-B: assigning to a new key adds it, and del removes a key

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

Command:

```bash
python3 -c "d={'a':1}; d['b']=2; del d['a']; print(d, len(d))"
```

Recorded output:

```
{'b': 2} 1
```

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

### TE-29-C: expected non-zero exit: del on a key that is not in the dict raises KeyError

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

Command:

```bash
python3 -c "d={}; del d['x']"
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "<string>", line 1, in <module>
KeyError: 'x'
```

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

TE-29-B: the dict started as {'a': 1}. After d['b']=2 and del d['a'] it printed {'b': 2} and length 1. TE-29-C: del d['x'] on an empty dict exited with status 1. The last line of standard error is KeyError: 'x'.

## Limits

Insertion order is preserved in current Python; not a promise about older versions.

## Source

- https://docs.python.org/3/tutorial/datastructures.html#dictionaries

## Evidence

- `TE-29-A`: a missing key raises KeyError; get returns a default instead

- 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-29-A` | a missing key raises KeyError; get returns a default instead | 0 | PASS | `374468456186` |
| `TE-29-B` | assigning to a new key adds it, and del removes a key | 0 | PASS | `b2eb070762fc` |
| `TE-29-C` | expected non-zero exit: del on a key that is not in the dict raises KeyError | 1 | PASS | `5b2a226e2baf` |

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 lists: indexes, slices and the IndexError](https://kairos-2z6s.onrender.com/res/python-list-basics-full-en)
- [Reading a failed Python test, line by line](https://kairos-2z6s.onrender.com/res/unittest-failure-full-en)
- [Python dict.get: read a key without KeyError](https://kairos-2z6s.onrender.com/res/python-dict-get-full-en)
- [Python Defaultdict: a dict that fills missing keys](https://kairos-2z6s.onrender.com/res/python-defaultdict-full-en)
- [Python Default Arguments: evaluated once](https://kairos-2z6s.onrender.com/res/python-default-arguments-full-en)
