✦ Python Foundations Collections module

Concept 11 · Dictionaries

Name each value.
Use a key to find it.

A dictionary keeps related information under meaningful keys. Use a key to find, update, or add the value it names.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

A dictionary finds related values through meaningful keys.

Lists organize values by position: position → value. A dictionary organizes one related group of information by meaningful names: key → value. For a student, student["name"] explains more clearly what we want than student[0] would.

student = {
    "name": "Ana",
    "age": 22,
    "course": "Python",
}

print(student["name"])
print(student["course"])

Curly braces {} hold comma-separated key: value pairs. Here "name" → "Ana", "age" → 22, and "course" → "Python". A key tells Python which related value to find. Read student["name"] as student → "name" → "Ana": "name" is a key, not a list position.

Ana Python

A dictionary is mutable, like a list. Assignment with an existing key changes that value: before, "age" → 22; after student["age"] = 23, "age" → 23. The key stays the same while its associated value changes.

↳

One assignment, two possible effects: When the key already exists, dictionary[key] = value updates its value. When the key is new, the same form adds a new key-value pair. A dictionary cannot keep two separate entries with the same key.

Update a value, then add a new key
student = {"name": "Ana", "age": 22}

student["age"] = 23
student["city"] = "Novi Sad"

print(student["age"])
print(student["city"])

The first assignment updates the existing "age" value. The second creates a new "city" → "Novi Sad" pair. The same bracket syntax is useful because the key tells Python whether it should update or add.

Membership checks dictionary keys
student = {"name": "Ana", "age": 22}

print("name" in student)
print("email" in student)

key in dictionary checks whether that key exists. The output is True and then False. It does not search values, so "Ana" in student is not a check for the value "Ana".

Count pairs and look up safely
student = {"name": "Ana", "age": 22, "course": "Python"}

print(len(student))
print(student.get("email"))
print(student.get("email", "Not provided"))

len(student) counts three key-value pairs. student["email"] would raise KeyError because that key does not exist. get() is a safe lookup: it returns None when the key is absent, or the default "Not provided" when you supply one.

Loop through keys and key-value pairs
student = {"name": "Ana", "age": 22}

for key in student:
    print(key, student[key])

for key, value in student.items():
    print(key, value)

A plain for key in student loop gives keys. Use student[key] to find each associated value. items() gives key-value pairs so Python can unpack them into the two variables key and value.

Trace dictionary state step by step
profile = {"name": "Ana", "points": 10}

profile["points"] = 15
profile["level"] = "beginner"

print(profile["points"])
print("level" in profile)
print(len(profile))

Trace the state before running: start with two pairs, update "points", then add "level". The lookup prints 15, membership for "level" is True, and the dictionary now has three pairs.

Choose a collection by the problem it solves

  1. Use a list for ordered values that may changeChoose a list when order or positions matter and the collection may grow, shrink, or be updated.
  2. Use a tuple for one fixed related groupChoose a tuple for stable related values whose positions have meaning, such as coordinates.
  3. Use a set for uniqueness and membershipChoose a set when duplicates should disappear and membership matters more than position.
  4. Use a dictionary for named related informationChoose a dictionary when values should be found through meaningful keys such as "name", "email", or "course".

Common beginner mistakes

  • Confusing a key with its valueIn student["name"], "name" is the key and "Ana" is its value. The key helps Python find the value.
  • Forgetting quotes around a string keystudent[name] looks for a variable called name. Write student["name"] when the key itself is the string "name".
  • Accessing a missing key without a planstudent["email"] raises KeyError when no "email" key exists. Use get() when a missing key is an expected possibility.
  • Thinking in checks values"name" in student checks keys. Do not assume that in automatically searches dictionary values.
  • Expecting duplicate keysAssigning a value to an existing key replaces that key’s value; it does not create a second independent copy of the key.

Python Playground

Loading Python runtime…

Before running the code, predict each output. Then update an existing value, add a new key-value pair, check membership, and watch how the dictionary changes.

dictionaries.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each operation before choosing an answer.

product = {"name": "Keyboard", "price": 2500}

product["price"] = 2200
product["stock"] = 5

print(product["price"])
print(len(product))
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.