Dictionaries: Keys, Values, and Lookup
Look up sample status codes by name, loop over the pairs, and print the same data as JSON text.
Based on the official Python documentation. Date checked: November 27, 2026. Script tested on Python 3.13.5.
Series: Python for small scripts, Part 6 of 10. Previous: Part 5: Lists: Store and Filter a Set of Items. Next: Part 7: Functions: Write a Reusable Check.
TL;DR
- A dictionary maps a key to a value, inside curly braces.
- This script looks up a sample status code, uses
.get()for a missing key, then prints every pair and a JSON copy of the same data. jsonis in the standard library. This script only prints text; it does not send a request.- Tested on Python 3.13.5. This is Part 6 of 10.
Before you start
- Python 3.11 or newer. This script was tested on Python 3.13.5. Check with
python3 --versionorpython --version. - Work in the
python-scriptsfolder from Part 1. Part 4 coveredforloops. - Run with
python3 codes.py. On Windows,python codes.pyis the usual form.
The finished script
Save this as codes.py in that folder:
import json
codes = {
"ok": 200,
"not_found": 404,
"error": 500,
}
print(codes["ok"])
print(codes.get("timeout", "unknown"))
for name, code in codes.items():
print(f"{name}: {code}")
print(json.dumps(codes, indent=2))
When you run it, you should see:
200
unknown
ok: 200
not_found: 404
error: 500
{
"ok": 200,
"not_found": 404,
"error": 500
}
Steps
- Save and run the script. Change into
python-scriptsand runpython3 codes.py. Why: you can see a direct lookup, a missing-key lookup, a loop, and a JSON listing in one output. You should see: the block above. - Read the dictionary and the two lookups.
"ok","not_found", and"error"are keys.codes["ok"]returns200.codes.get("timeout", "unknown")returns the fallback"unknown"because"timeout"is not a key. Why: square brackets raise an error when the key is missing..get()is the safer choice when a key may be absent. - Read the
.items()loop. Each pass setsnameto a key andcodeto its value. Since Python 3.7, a dictionary keeps insertion order, so the lines print in the same order you wrote the keys. Why: this is the usual way to walk both the names and the values. - Read the JSON line.
json.dumpsbuilds a text version of the dictionary.indent=2adds line breaks and spaces so the text is easier to read. Why: configuration files and many HTTP APIs use this shape. Thejsonmodule is in the standard library, so you do not install anything in this part.
If it fails
KeyError: 'timeout'. A line such asprint(codes["timeout"])looked up a key that is not in the dictionary:
Traceback (most recent call last):
File "keyerror.py", line 2, in <module>
print(codes["timeout"])
~~~~~^^^^^^^^^^^
KeyError: 'timeout'
Use codes.get("timeout", "unknown") when the key may be missing, or check if "timeout" in codes: first.
NameError: name 'json' is not defined. Theimport jsonline is missing. Add it at the top of the file and run the script again.- The loop prints only the keys. The loop may be
for name in codes:rather thanfor name, code in codes.items(). Use.items()when you need both.
Stay safe
- The keys and codes in this script are samples. They do not come from a live service.
- Do not store passwords, tokens, or keys in a dictionary you print or write to a file.
json.dumpsonly builds text. This script does not open a network connection.
Next
Part 7: Functions: Write a Reusable Check
Sources
- Python docs, “Dictionaries” (docs.python.org/3/tutorial/datastructures.html#dictionaries), Python 3.15.2 documentation, checked November 27, 2026.
- Python docs, “json — JSON encoder and decoder” (docs.python.org/3/library/json.html), checked November 27, 2026.
Spot something wrong or outdated? Leave a comment below. Every comment is reviewed to keep these posts accurate.
By commenting, you agree to our Terms of use and confirm this is your own work. Short quotes are fine with a credit and link. Copied, unsafe, or abusive comments are removed. See something copied? Email [email protected].