Stockertim Dictionary · Technology
Python dictionary
Also called Python dict, dict in Python, key-value mapping
In short
A Python collection of key-value pairs written in curly brackets, used to look up a value, such as a price, directly by its key, such as a product code.
- Beginner
- Concept
- Checked
In detail
A dictionary maps keys to values. prices = {'TEA': 1500, 'MUG': 4000, 'POT': 9000} creates one with three entries, and prices['MUG'] returns 4000. Unlike a list, which is reached by position, a dictionary is reached by a meaningful key, which makes code that looks things up easy to read.
Keys must be unique and of a type that cannot change, typically text or numbers; values can be anything, including lists or other dictionaries. Assigning to a key either updates an existing entry or adds a new one: prices['CUP'] = 2500 adds a fourth item. Since Python 3.7 a dictionary keeps its items in the order they were inserted.
Asking for a key that does not exist raises a KeyError. Code that may meet missing keys uses prices.get('JUG') instead, which returns None, or prices.get('JUG', 0) to supply a default. The keyword in tests for a key, as in 'TEA' in prices, and loops over prices.items() visit each key with its value.
Dictionaries are the code equivalent of a lookup table in a spreadsheet, and they are fast even with many entries because the key is found directly rather than by scanning. Data from web services usually arrives as JSON, which Python reads as nested dictionaries and lists. The main traps are duplicate keys, where a later entry overwrites an earlier one without warning, and keys that differ only in case or spacing.
Worked example
A stationery shop in Nairobi prices an order of codes using a dictionary of unit prices in shillings.
prices = {'PEN': 50, 'BOOK': 200, 'FILE': 150}- The order is 10 pens, 5 books and 4 files.
- Total: 10 × 50 + 5 × 200 + 4 × 150 = 2,100
Each line of the order looks up its price by code, and the order totals KSh 2,100.
In practice
Analysts use dictionaries to map codes to names, rename columns in pandas, count occurrences of each category and hold settings for a script. Reading configuration or API results almost always means working with dictionaries.
Watch out
- A repeated key overwrites the earlier value silently, so the dictionary ends up shorter than the source list.
prices['X']fails on a missing key; useget()with a default when gaps are expected.
Learn it properly
Official sources
- Quality assurance of code for analysis and research · Government Analysis Function
- API technical and data standards · Government Digital Service
Facts last checked on 9 November 2026.
Education, not advice: the Stockertim Dictionary explains what terms mean. It never recommends buying or selling anything.