Python Dictionaries

Let’s look at dictionaries

  1. Notice

    A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
scores = {"Ada": 3}
scores["Ada"] = scores["Ada"] + 1
scores["Ben"] = 5
print(scores)
Live state

Top-level scope

scores→Object 1
Object 1 · dict{'Ada': 4, 'Ben': 5}
This step

A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.

Look up a value by its key

A dictionary connects keys to values. A lookup uses the key, rather than a numeric position.

Image brief

Alt text
A dictionary maps the key name to Ada and the key language to Python; looking up name returns Ada.
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Dictionaries". Teaching focus: A dictionary connects keys to values. A lookup uses the key, rather than a numeric position. Depict exactly the relationships and labels in this reference diagram: DICTIONARY +-------------+-------------+ | KEY | VALUE | +-------------+-------------+ | "name" | "Ada" | | "language" | "Python" | +-------------+-------------+ record["name"] --> "Ada" Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A dictionary maps the key name to Ada and the key language to Python; looking up name returns Ada.

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
scores = {"Ada": 3}
scores["Ada"] = scores["Ada"] + 1
scores["Ben"] = 5
print(scores)
Live state

Top-level scope

scores→Object 1
Object 1 · dict{'Ada': 4, 'Ben': 5}
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
{'Ada': 4, 'Ben': 5}

What do you expect this program to print?

What does this example display?

scores = {"Ada": 3}
scores["Ada"] = scores["Ada"] + 1
scores["Ben"] = 5
print(scores)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

{'Ada': 4, 'Ben': 5}

Follow the printed values in order. The output is: {'Ada': 4, 'Ben': 5}

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.

A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.

What is a Python Dictionary?

A Python dictionary is an unordered collection that stores data as key-value pairs. Each key is unique, and each key maps to a value. You can use the key to access the associated value. Think of it as a real-world dictionary where you look up a word (key) and get its meaning (value).

Example of a Dictionary

my_dict = {
    "name": "Arjun",
    "age": 25,
    "city": "New York"
}

In this dictionary, the keys are "name", "age", and "city". The corresponding values are "Arjun", 25, and "New York".

Where Are Dictionaries Used?

Dictionaries are very useful when you need to store data that is associated with a unique identifier (the key). For example, storing student names with their grades, a person's contact information (name, phone number, etc.), or the countries and their capitals.

Basic Operations on Dictionaries

1. Create a Dictionary

To create a dictionary, you can use curly braces {} and separate keys and values with a colon :.

student = {
    "name": "Arjun",
    "age": 22,
    "grade": "A"
}

2. Read/Access Data

To get the value associated with a key, you can use square brackets [] with the key.

student = {
    "name": "Arjun",
    "age": 22,
    "grade": "A"
}

print(student["name"])
Arjun

3. Update Data

If you want to change the value associated with a key, you can directly assign a new value to that key.

student = {
    "name": "Arjun",
    "age": 22,
    "grade": "A"
}

student["age"] = 23 
print(student)
{'name': 'Arjun', 'age': 23, 'grade': 'A'}

4. Add New Key-Value Pair

You can add a new key-value pair to the dictionary by assigning a value to a new key.

student = {
    "name": "Arjun",
    "age": 22,
    "grade": "A"
}

student["city"] = "London"
print(student)
{'name': 'Arjun', 'age': 22, 'grade': 'A', 'city': 'London'}

5. Delete Data

If you want to remove a key-value pair from the dictionary, you can use the del keyword.

student = {
    "name": "Arjun",
    "age": 22,
    "grade": "A"
}
    
del student["grade"]
print(student)
{'name': 'Arjun', 'age': 22}

Summary of CRUD Operations

  • Create: my_dict = {} to create a dictionary.
  • Read: my_dict[key] to access a value by key.
  • Update: my_dict[key] = new_value to update a value.
  • Add: my_dict[new_key] = new_value to add a new key-value pair.
  • Delete: del my_dict[key] to delete a key-value pair.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.

Watch out for this idea

Assigning the same key again adds a duplicate key.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Tuples →