Technical skill practice

Python dict: Build and Update a Station Lookup

A Python dict maps keys to values. Use a key to look up its associated value rather than treating the mapping as a list of positions. Assigning an existing key replaces its value; assigning a new key adds another entry. The small station lookup below makes those operations visible.

Start with a small, explicit task

Build a lookup from fictional station codes to display labels. Correct one label and add a station. Then compare the mapping’s size and ordered keys with the original. The codes are deliberately strings, including a numeric-looking code, to show that an identifier’s type affects lookup.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_dictionary.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

South entrance

['N1', 'S2', '03', 'E4']

Workshop

stations = {"N1": "North", "S2": "South", "03": "Workshop"}
stations["S2"] = "South entrance"
stations["E4"] = "East"
assert stations["S2"] == "South entrance"
assert len(stations) == 4
assert list(stations) == ["N1", "S2", "03", "E4"]
assert "03" in stations and 3 not in stations
assert stations.get("missing", "Unknown") == "Unknown"
print(stations["S2"])
print(list(stations))
print(stations["03"])

Explain the result

Replacing S2 changes its label but leaves the number of keys unchanged. Adding E4 increases the mapping to four entries. Iterating this dictionary produces keys in insertion order; updating S2 does not move it to the end. That order is not alphabetical sorting, so sort explicitly if a report requires a sorted display.

The string key 03 exists, while the integer 3 does not. The distinction matters for imported identifiers: converting a code to a number can change what record you are asking for. get supplies Unknown for an absent key in this example. Direct bracket lookup is more appropriate when a missing required key should be treated as an error rather than silently defaulted.

Avoid these mistakes

Dictionary keys need to be hashable; a mutable list is not a usable key. Also remember that repeated equal keys cannot store separate entries: the later assignment replaces the earlier value. If one station needs several labels, model the value as a collection instead of repeating the same key.

Questions, answered

Is a dictionary indexed from zero?

Its lookup uses keys. A key can be zero, but that is a chosen key rather than the first position.

Does assigning a duplicate key add a second entry?

No. In this example updating S2 replaces that entry’s value.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.