Course Progress28%
🍎 Python Basics Topic 28 / 100
⏳ 5 min read

Dictionaries

Key-value storage — look up any value instantly by its name, not by its position. Python's most powerful built-in data structure.

"A list uses a position number to find things. A dictionary uses a name. That one change makes everything faster and more readable."

— ShurAI

The Problem Dictionaries Solve

Imagine storing a student's profile. With a list, you must remember that index 0 is name, index 1 is age — fragile and confusing. With a dictionary, you just use the name directly:

python — list vs dict
# List approach — must remember what each index means
student = ["Riya", 22, "Mumbai", 88]
print(student[3])   # what is index 3 again?

# Dictionary approach — meaning is built in
student = {"name": "Riya", "age": 22, "city": "Mumbai", "score": 88}
print(student["score"])  # instantly clear

Creating a Dictionary

Use curly braces {}. Each entry is a key: value pair separated by commas. Keys are usually strings, values can be anything:

python
# A person's contact card
contact = {
    "name"  : "Arjun Mehta",
    "phone" : "98765-43210",
    "city"  : "Delhi",
    "active": True
}

# A product in a shop
product = {
    "name"    : "Basmati Rice",
    "price"   : 280,
    "stock"   : 150,
    "category": "Grocery"
}

print(type(contact))   # <class 'dict'>
contact = { "name": "Arjun", "phone": "98765...", "city": "Delhi" }
"name"
"Arjun Mehta"
"phone"
"98765-43210"
"city"
"Delhi"
Blue = key  ·  White = value  ·  Access any value instantly using its key

Reading, Adding, and Updating

python
student = {"name": "Sneha", "score": 78}

# Read a value
print(student["name"])      # Sneha

# Update a value
student["score"] = 85
print(student["score"])     # 85

# Add a new key-value pair
student["grade"] = "B"
print(student)
# {'name': 'Sneha', 'score': 85, 'grade': 'B'}

# Delete a key
del student["grade"]
print(student)
# {'name': 'Sneha', 'score': 85}

Looping Over a Dictionary

python
product = {"name": "Rice", "price": 280, "stock": 50}

# Loop over keys
for key in product:
    print(f"{key}: {product[key]}")

# Loop over key-value pairs (most common)
for key, value in product.items():
    print(f"{key:10} = {value}")

Checking if a Key Exists

python
scores = {"Riya": 88, "Arjun": 75}

if "Riya" in scores:
    print(f"Riya scored {scores['Riya']}")   # Riya scored 88

if "Sneha" not in scores:
    print("Sneha not found")

Real Example — Phone Book

python
phonebook = {
    "Riya"   : "98765-11111",
    "Arjun"  : "98765-22222",
    "Sneha"  : "98765-33333",
    "Vikram" : "98765-44444",
}

while True:
    name = input("Search name (or 'quit'): ").strip().title()
    if name == "Quit":
        break
    if name in phonebook:
        print(f"  {name}: {phonebook[name]}")
    else:
        print(f"  '{name}' not found.")

"Whenever you find yourself using index 0, 1, 2 to access related data, ask: should this be a dictionary instead?"

— ShurAI

🧠 Quiz — Q1

How do you create a dictionary in Python?

🧠 Quiz — Q2

Given d = {"name": "Riya", "age": 22}, how do you get the name?

🧠 Quiz — Q3

How do you add a new key "city" with value "Pune" to dict d?

🧠 Quiz — Q4

What does "phone" in contact check?