Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner18 min

Visual reference

Unlock the power of Python dictionariesUnlock the power of Python dictionaries
Dictionaries — key–value mappingsDictionaries — key–value mappings
Python data structures including dictPython data structures including dict

What you will learn

  • What a dictionary is and how it differs from lists
  • Create dictionaries with {} and dict()
  • 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

Name = key, number = value
python
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 {}

python
fruits = {
    10: "Apple",
    20: "Mango",
    30: "Kiwi",
    40: "Strawberry",
}

print(fruits)
text
{10: 'Apple', 20: 'Mango', 30: 'Kiwi', 40: 'Strawberry'}

Also common:

python
d2 = {"Python": 1, "C": 2, "Java": 3, "C++": 4}

Method 2: Empty dictionary

python
d1 = {}
d2 = dict()

print(type(d1))
text
<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.

Keys on the left map to values on the right
python
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:

python
fruits = {
    10: "Apple",
    20: "Mango",
}

print(fruits[10])
print(fruits[20])
text
Apple
Mango

Missing key

python
# print(fruits[50])
# KeyError: 50

Important

0 is not the first slot in a dict.

python
# fruits[0]
# KeyError — unless 0 is actually a key

Your 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.

python
d = {
    10: 1.2,
    10: 2.3,
    10: 4.5,
}

print(d)
text
{10: 4.5}

Another pattern from your notes:

python
d1 = {10: 1.2, 20: 2.3, 10: 1.9, 80: 2.9}
print(d1)
text
{10: 1.9, 20: 2.3, 80: 2.9}
Same key — last value wins

Memory trick: same key → last value wins.

Chapter 7 — Duplicate values

Values may repeat; only keys must differ.

python
marks = {
    "Math": 90,
    "Science": 90,
    "English": 85,
}

print(marks)
text
{'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

python
fruits = {
    10: "Apple",
    20: "Mango",
}

fruits[10] = "Guava"

print(fruits)
text
{10: 'Guava', 20: 'Mango'}

Add a new key

python
fruits[500] = 5.5

print(fruits)
text
{10: 'Guava', 20: 'Mango', 500: 5.5}

Your notes: id(d1) often stays the same after updates — same dict object, new or changed entries.

Before and after update + new key

Chapter 9 — Building dictionaries dynamically

Start empty and grow with assignments:

python
d = {}

d[100] = 1.2
d[200] = 2.2
d[300] = 1.2

print(d)
text
{100: 1.2, 200: 2.2, 300: 1.2}

Update when the key already exists:

python
d[300] = 0.2

print(d)
text
{100: 1.2, 200: 2.2, 300: 0.2}

Syntax from your notes:

text
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
List uses index 0,1 — dict uses keys 10,20

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

python
phone = {
    "Rahul": 9876543210,
    "Priya": 9123456789,
    "Amit": 9988776655,
}

print(phone)

2. Update and add

python
phone["Rahul"] = 9000000000
phone["Neha"] = 9555555555

print(phone)

3. Predict the output

python
d = {
    1: "A",
    1: "B",
}

print(d)
text
{1: 'B'}

Quick knowledge check

  1. Are dictionary keys unique?
    Yes.

  2. What happens if a key doesn't exist?
    d[99] raises KeyError.

  3. Does {} create a set?
    No — empty dict. Use set() 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] = value on empty or non-empty dicts.

Next lesson: NoneType — the None value (not the same as 0, False, or empty collections).

Loading playground…