Dictionaries
A dictionary is a collection of key-value pairs, where you look a value up by its key instead of by its position.
Last updated: 30 Sep, 2026 · Python 3.14
A ticket is more than its text: it has an id and a customer too. A list would keep them by position; a dictionary gives each one a name.
print(). In a .py file and in the editor here, wrap it in print(), as the run below does.my_dict = {"Car1": "Audi", "Car2": "BMW", "Car3": "Mercedes Benz"}
print(my_dict["Car1"])
for x in my_dict:
print(x)
for x in my_dict.values():
print(x)
for x in my_dict.items():
print(x)
my_dict["car4"] = "Audi 2.0"
my_dict["Car1"] = "Maruti"
print(my_dict)Audi
Car1
Car2
Car3
Audi
BMW
Mercedes Benz
('Car1', 'Audi')
('Car2', 'BMW')
('Car3', 'Mercedes Benz')
{'Car1': 'Maruti', 'Car2': 'BMW', 'Car3': 'Mercedes Benz', 'car4': 'Audi 2.0'}Looping over a dictionary gives its keys; values() gives the values and items() gives both as pairs. A new key, car4, was added at the end, and storing under Car1 again replaced Audi with Maruti. The spelling of the car names is tidied from the notebook. A ticket works the same way, with a key for each field.
Syntax:
record = {"key": value, "other_key": other_value}
record["key"] # read a value
record["new_key"] = x # add or replaceCreating a dictionary and reading values
ticket = {"id": 1, "customer": "Asha", "text": "I was charged twice for one order"}
print(ticket["text"])
print(ticket["customer"])I was charged twice for one order Asha
Curly brackets make a dictionary. Each entry is a key, a colon, and a value. You read a value by putting its key in square brackets, the way a list takes a position.
Adding and changing values
ticket["category"] = "billing"
ticket["customer"] = "Asha K."
print(ticket){'id': 1, 'customer': 'Asha K.', 'text': 'I was charged twice for one order', 'category': 'billing'}Storing under a key that does not exist adds it. Storing under one that does replaces the value. A key appears only once in a dictionary.
Reading a key that is not there
print(ticket["priority"])Traceback (most recent call last):
File "main.py", line 1, in <module>
print(ticket["priority"])
KeyError: 'priority'A missing key is a KeyError. When a key may or may not be there, use get, which gives back a value you choose instead of stopping the program. Delete that line before you add the next example: an error stops the file where it happens.
print(ticket.get("priority"))
print(ticket.get("priority", 3))
print(ticket.get("customer", "unknown"))None 3 Asha K.
None is Python's value for nothing here. get gives it back when you do not name a default.
Looping over keys and values
for key, value in ticket.items():
print(key, "=", value)id = 1 customer = Asha K. text = I was charged twice for one order category = billing
items gives each key with its value, in the order they were added. keys() gives only the keys and values() only the values.
ticket["key"] vs ticket.get("key")
ticket["priority"] | ticket.get("priority", 3) | |
|---|---|---|
| Key present | Its value | Its value |
| Key missing | KeyError, the program stops | The default, 3 |
| Use when | The key must be there | The key is optional |
Where dictionaries show up in AI code
- A single chat message:
{"role": "user", "content": "..."}. - Settings passed to a model call, such as a temperature and a token limit.
- The parsed answer of a model that replied in JSON.
ticket["Text"] is a KeyError when the key is "text", because capital letters count.Related
- Previous: for loops
- Next: Lists of dictionaries
- Reference: Dictionaries in the Python tutorial
- Add
ticket["priority"] = 4above the threegetlines and run them again. - Print
list(ticket.keys()). - Print
"text" in ticket. On a dictionary,inchecks the keys.
This is what real progress feels like.