Lesson 13
dict — Key–Value Mapping
Learn Python's dictionary data type to store data as key–value pairs, perform fast lookups, update values, and build real-world mappings like phone books and user profiles.
Visual reference
What you will learn
- What a dictionary is and how it differs from lists
- Create dictionaries with
{}anddict() - Access values using keys
- Update existing values and add new entries
- Unique keys and duplicate values
Chapter 1 — What is a dictionary?
A dictionary (dict) stores data as key–value pairs.
Instead of finding data by position (like list[0]), you look it up by key — like a name in a phone book.
Purpose (your notes): store data in the form (key, value). Keys must be unique; values may or may not repeat.
Syntax: varname = {Key1: Val1, Key2: Val2, ..., Key-n: Val-n}
Real-world analogy: phone book
student = {
"name": "Rahul",
"age": 21,
"city": "Hyderabad",
}- Key →
"name" - Value →
"Rahul"
Chapter 2 — Dictionary properties
| Property | Behavior |
|---|---|
| Stores | Key : value pairs |
| Order | Insertion order preserved (Python 3.7+) |
| Mutable | Yes — add, update, delete entries |
| Duplicate keys | Not allowed (last wins in a literal) |
| Duplicate values | Allowed |
Indexing d[0] |
No — unless 0 is an actual key |
| Slicing | Not supported |
Your notes: dict objects have no indices like sequences. Keys should be immutable / hashable (e.g. int, str, tuple); values can be any type and change freely.
Chapter 3 — Creating dictionaries
Method 1: Using {}
fruits = {
10: "Apple",
20: "Mango",
30: "Kiwi",
40: "Strawberry",
}
print(fruits){10: 'Apple', 20: 'Mango', 30: 'Kiwi', 40: 'Strawberry'}Also common:
d2 = {"Python": 1, "C": 2, "Java": 3, "C++": 4}Method 2: Empty dictionary
d1 = {}
d2 = dict()
print(type(d1))<class 'dict'>Note: {} is an empty dict, not a set — use set() for an empty set.
Chapter 4 — Understanding keys and values
Each key maps to exactly one value.
employee = {
101: "Alice",
102: "Bob",
103: "Carol",
}| Key | Value |
|---|---|
| 101 | Alice |
| 102 | Bob |
| 103 | Carol |
Chapter 5 — Accessing values
Use the key, not a position:
fruits = {
10: "Apple",
20: "Mango",
}
print(fruits[10])
print(fruits[20])Apple
MangoMissing key
# print(fruits[50])
# KeyError: 50Important
0 is not the first slot in a dict.
# fruits[0]
# KeyError — unless 0 is actually a keyYour notes: d1[0] on {10: "Apple", ...} raises KeyError: 0.
Chapter 6 — Duplicate keys
Keys must be unique. If the same key appears more than once in a literal, Python keeps the last value.
d = {
10: 1.2,
10: 2.3,
10: 4.5,
}
print(d){10: 4.5}Another pattern from your notes:
d1 = {10: 1.2, 20: 2.3, 10: 1.9, 80: 2.9}
print(d1){10: 1.9, 20: 2.3, 80: 2.9}Memory trick: same key → last value wins.
Chapter 7 — Duplicate values
Values may repeat; only keys must differ.
marks = {
"Math": 90,
"Science": 90,
"English": 85,
}
print(marks){'Math': 90, 'Science': 90, 'English': 85}Example from notes: {100: 1.2, 200: 1.3, 300: 1.2, 400: 1.3} — value 1.2 appears twice.
Chapter 8 — Updating and adding data
Dictionaries are mutable. Assignment with dictobj[key] = value either updates an existing key or inserts a new pair.
Update an existing key
fruits = {
10: "Apple",
20: "Mango",
}
fruits[10] = "Guava"
print(fruits){10: 'Guava', 20: 'Mango'}Add a new key
fruits[500] = 5.5
print(fruits){10: 'Guava', 20: 'Mango', 500: 5.5}Your notes: id(d1) often stays the same after updates — same dict object, new or changed entries.
Chapter 9 — Building dictionaries dynamically
Start empty and grow with assignments:
d = {}
d[100] = 1.2
d[200] = 2.2
d[300] = 1.2
print(d){100: 1.2, 200: 2.2, 300: 1.2}Update when the key already exists:
d[300] = 0.2
print(d){100: 1.2, 200: 2.2, 300: 0.2}Syntax from your notes:
dictobj[Key1] = Val1
dictobj[Key2] = Val2
...Chapter 10 — Dictionary vs list
| Feature | list |
dict |
|---|---|---|
| Access by | Index (0, 1, …) |
Key |
| Example | lst[0] |
d["name"] |
| Stores | Values only | Key–value pairs |
| Duplicate keys | — | No |
| Duplicate values | Yes | Yes |
| Mutable | Yes | Yes |
Use a dict when you look up by meaningful keys (name, id, code). Use a list when order by position matters.
Practice exercises
1. Create a phone book
phone = {
"Rahul": 9876543210,
"Priya": 9123456789,
"Amit": 9988776655,
}
print(phone)2. Update and add
phone["Rahul"] = 9000000000
phone["Neha"] = 9555555555
print(phone)3. Predict the output
d = {
1: "A",
1: "B",
}
print(d){1: 'B'}Quick knowledge check
-
Are dictionary keys unique?
Yes. -
What happens if a key doesn't exist?
d[99]raisesKeyError. -
Does
{}create a set?
No — empty dict. Useset()for an empty set.
Summary
- A dictionary stores key–value pairs for fast lookup by key.
- Keys must be unique and hashable; values can repeat and be any type.
- Access with
d[key], not numeric indexes (unless that number is a key). - Mutable — update with
d[key] = valueon empty or non-empty dicts.
Next lesson: NoneType — the None value (not the same as 0, False, or empty collections).
Loading playground…