I1 · Python Programming for AI

Dictionaries

A dictionary stores key-value pairs: each unique key maps to a value, such as {"Riya": 91}. Values are looked up by key rather than by position, very quickly. Dictionaries are changeable, keep insertion order, and their values can themselves be lists or dictionaries.

12 min Intermediate Jump to the activity

What you need: The earlier topics of this module.

Definition

How does a phone book find a number instantly, without reading every page?

A dictionary stores key-value pairs: each unique key maps to a value, such as {"Riya": 91}. Values are looked up by key rather than by position, very quickly. Dictionaries are changeable, keep insertion order, and their values can themselves be lists or dictionaries.

In simple words

A dictionary is like a real dictionary: look up a word (the key) and get its meaning (the value). marks["Riya"] gives Riya's marks. marks["Kabir"] = 78 adds or updates Kabir. To count words, use each word as a key and its count as the value.

Why it matters

Dictionaries are everywhere in AI code: word counts, settings for a model, a row of data with named columns, and the JSON that web APIs and LLMs send back.

How it works

OperationExample
Look upmarks["Riya"]
Look up safely, with a defaultmarks.get("Zara", 0)
Add or updatemarks["Kabir"] = 78
Check a key"Riya" in marks
Loop over pairsfor name, m in marks.items():
Removedel marks["Riya"]

In code

Counting words with a dictionary, then a nested dictionary for a student record.

word_counter.py
text = "ai helps people and people build ai and ai learns"
counts = {}
for word in text.split():
    counts[word] = counts.get(word, 0) + 1
print(counts)

most = max(counts, key=counts.get)
print("Most common word:", most, "appears", counts[most], "times")

student = {"name": "Meera", "marks": {"maths": 91, "science": 88}}
print(student["name"], "scored", student["marks"]["maths"], "in maths")
for subject, score in student["marks"].items():
    print(" ", subject, score)

Output

{'ai': 3, 'helps': 1, 'people': 2, 'and': 2, 'build': 1, 'learns': 1}
Most common word: ai appears 3 times
Meera scored 91 in maths
  maths 91
  science 88

Try it yourself

Predict the lookup

For ages = {"Aarav": 14, "Sana": 16}, what does each give?

  • ages["Sana"]
  • ages.get("Zoya", 0)
  • "Aarav" in ages
  • len(ages)
  • ages["Zoya"]

Common mistakes

  • Using [ ] for a key that may be missing.
  • Using a list as a key. Keys must be unchangeable, such as strings, numbers or tuples.
  • Expecting two equal keys. A new value for an existing key replaces the old one.

Check yourself

Question 1

1. How are values found in a dictionary?

Question 2

2. What does d.get("x", 0) do when "x" is missing?

Question 3

3. Which loop gives both keys and values?

Summary

  • Dictionaries map unique keys to values for fast lookup.
  • Use .get() for keys that may be missing.
  • Loop with .items(); nest dictionaries for structured records.

Finished reading?

Sources: Python docs: Data Structures