Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

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.

Creating, reading and updating a dictionary · from Tutorial 3- Python Sets, Dictionaries and Tuples · 6:01 to 11:27
The notebook in the video describes a dictionary as unordered. Since Python 3.7 a dictionary keeps the order its keys were added, as the run below shows. The video runs each line in a Jupyter notebook, which shows the value of a cell's last line without print(). In a .py file and in the editor here, wrap it in print(), as the run below does.
ExampleFrom the video, run here
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)

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:

python
record = {"key": value, "other_key": other_value}
record["key"]            # read a value
record["new_key"] = x    # add or replace

Creating a dictionary and reading values

Example
ticket = {"id": 1, "customer": "Asha", "text": "I was charged twice for one order"}
print(ticket["text"])
print(ticket["customer"])

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

Example
ticket["category"] = "billing"
ticket["customer"] = "Asha K."
print(ticket)

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

Example
print(ticket["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.

Example
print(ticket.get("priority"))
print(ticket.get("priority", 3))
print(ticket.get("customer", "unknown"))

None is Python's value for nothing here. get gives it back when you do not name a default.

Looping over keys and values

Example
for key, value in ticket.items():
    print(key, "=", value)

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 presentIts valueIts value
Key missingKeyError, the program stopsThe default, 3
Use whenThe key must be thereThe 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.
Watch out. Keys are exact. ticket["Text"] is a KeyError when the key is "text", because capital letters count.
Try it yourself
  • Add ticket["priority"] = 4 above the three get lines and run them again.
  • Print list(ticket.keys()).
  • Print "text" in ticket. On a dictionary, in checks the keys.
Previousfor loops

This is what real progress feels like.