🕯️ Magic Note
Dictionaries are also called “hash maps” or “associative arrays” in other languages. Python dictionaries are incredibly fast at looking up values by key. No matter how many items are in the dictionary, finding a key takes almost the same time. This is why they are used everywhere.
Python
# Empty dictionary
empty = {}
# Dictionary with string keys and integer values
ages = {“Ali”: 25, “Sara”: 30, “Reza”: 28}
# Dictionary with mixed key types
mixed = {“name”: “Feloriya”, 42: “answer”, 3.14: “pi”}
# Dictionary can hold any type of values
person = {
“name”: “Feloriya”,
“age”: 25,
“skills”: [“Python”, “Web Design”, “SEO”],
“is_active”: True
}
# Using the dict() constructor
fruits = dict(apple=5, banana=3, cherry=8)
# {‘apple’: 5, ‘banana’: 3, ‘cherry’: 8}
Python
ages = {“Ali”: 25, “Sara”: 30, “Reza”: 28}
print(ages[“Ali”]) # 25
print(ages[“Sara”]) # 30
# This causes KeyError
# print(ages[“Mehdi”]) # KeyError: ‘Mehdi’
Python
ages = {“Ali”: 25, “Sara”: 30, “Reza”: 28}
print(ages.get(“Ali”)) # 25
print(ages.get(“Mehdi”)) # None (no error)
print(ages.get(“Mehdi”, 0)) # 0 (custom default)
print(ages.get(“Sara”, 0)) # 30 (ignores default because key exists)
Python
ages = {“Ali”: 25, “Sara”: 30}
print(ages) # {‘Ali’: 25, ‘Sara’: 30}
# Change an existing key
ages[“Ali”] = 26
print(ages) # {‘Ali’: 26, ‘Sara’: 30}
# Add a new key-value pair
ages[“Reza”] = 28
print(ages) # {‘Ali’: 26, ‘Sara’: 30, ‘Reza’: 28}
🕯️ Magic Note
There is no “add” method. The same syntax dictionary[key] = value both adds (if key is new) and updates (if key exists). This is simple and intentional.
Python
person = {“name”: “Feloriya”, “age”: 25, “city”: “Tehran”}
# .pop() removes and returns the value
age = person.pop(“age”)
print(age) # 25
print(person) # {‘name’: ‘Feloriya’, ‘city’: ‘Tehran’}
# del removes without returning
del person[“city”]
print(person) # {‘name’: ‘Feloriya’}
# .popitem() removes and returns the last inserted pair (Python 3.7+)
person[“age”] = 25
person[“city”] = “Tehran”
last = person.popitem()
print(last) # (‘city’, ‘Tehran’)
# .clear() removes everything
person.clear()
print(person) # {}
Python
ages = {“Ali”: 25, “Sara”: 30, “Reza”: 28}
print(“Ali” in ages) # True
print(“Mehdi” in ages) # False
print(“Ali” not in ages) # False
print(“Mehdi” not in ages) # True
# Safely remove a key if it exists
if “Ali” in ages:
del ages[“Ali”]
Python
person = {“name”: “Feloriya”, “age”: 25, “skill”: “Python”}
# All keys
print(person.keys()) # dict_keys([‘name’, ‘age’, ‘skill’])
# All values
print(person.values()) # dict_values([‘Feloriya’, 25, ‘Python’])
# All key-value pairs as tuples
print(person.items()) # dict_items([(‘name’, ‘Feloriya’), (‘age’, 25), (‘skill’, ‘Python’)])
# Convert to a list if needed
keys_list = list(person.keys())
print(keys_list) # [‘name’, ‘age’, ‘skill’]
🕯️ Magic Note
In Python 3.7 and later, dictionaries preserve insertion order. This means .keys(), .values(), and .items() return items in the order you added them. Before Python 3.7, dictionaries were unordered.
Python
ages = {“Ali”: 25, “Sara”: 30, “Reza”: 28}
# Looping through keys (default)
for name in ages:
print(name)
# Output: Ali, Sara, Reza
# Looping through values
for age in ages.values():
print(age)
# Output: 25, 30, 28
# Looping through both key and value (most common)
for name, age in ages.items():
print(f”{name} is {age} years old”)
# Ali is 25 years old
# Sara is 30 years old
# Reza is 28 years old
Python
person = {“name”: “Feloriya”, “age”: 25, “skill”: “Python”}
print(len(person)) # 3
empty = {}
print(len(empty)) # 0
print(not empty) # True (empty dict is falsy)
Python
dict1 = {“a”: 1, “b”: 2}
dict2 = {“c”: 3, “d”: 4}
# Python 3.9+ (creates a new dictionary)
merged = dict1 | dict2
print(merged) # {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
# .update() modifies the original
dict1.update(dict2)
print(dict1) # {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
# For Python 3.5+ (creating a new dictionary)
merged = {**dict1, **dict2}
Python
users = {
“ali”: {
“age”: 25,
“email”: “ali@example.com”,
“score”: 95
},
“sara”: {
“age”: 30,
“email”: “sara@example.com”,
“score”: 88
}
}
print(users[“ali”][“email”]) # ali@example.com
print(users[“sara”][“score”]) # 88
# Loop through nested dictionary
for username, info in users.items():
print(f”User: {username}, Age: {info[‘age’]}”)
Python
# Create a dictionary of squares: {1:1, 2:4, 3:9, 4:16, 5:25}
squares = {x: x**2 for x in range(1, 6)}
print(squares) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# Filtered comprehension
even_squares = {x: x**2 for x in range(1, 11) if x % 2 == 0}
print(even_squares) # {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
# Swapping keys and values
original = {“a”: 1, “b”: 2, “c”: 3}
swapped = {v: k for k, v in original.items()}
print(swapped) # {1: ‘a’, 2: ‘b’, 3: ‘c’}
Python
# Valid keys (immutable types)
valid = {
“string”: 1,
42: 2,
3.14: 3,
(1, 2): 4, # tuple is immutable
True: 5
}
# Invalid keys (mutable types) – will cause TypeError
# invalid = {[1, 2]: “list”} # TypeError: unhashable type: ‘list’
# invalid = {{“a”: 1}: “dict”} # TypeError: unhashable type: ‘dict’
- Forgetting that dictionary keys must be immutable (strings, numbers, tuples)
- Using square brackets with a missing key instead of .get()
- Assuming dictionaries preserve order in old Python versions (pre-3.7)
- Modifying a dictionary while iterating over it (causes errors)
- Confusing in for keys vs values: value in dict checks keys, not values
- Using .pop() without a default and the key is missing
- How do you create a dictionary mapping “apple” to 5 and “banana” to 3?
- What is the difference between dict[“key”] and dict.get(“key”)?
- How do you check if a key exists in a dictionary?
- Write a loop that prints both the name and age from ages = {“Ali”: 25, “Sara”: 30}
- What happens if you try to use a list as a dictionary key?
- How do you merge two dictionaries into a new one?
⚡ Whisper
A dictionary is a book of whispers. Each whisper has a caller (the key) and a secret (the value). You do not need to remember the page number. You only need to remember the caller’s name. Speak the key, and Python finds the whisper instantly, even among thousands. This is the conjure of mapping. This is how you organize chaos into meaning. Build your dictionaries carefully. Choose keys that make sense. Let each value hold exactly what it promises. And when you need a whisper back, you will find it waiting exactly where you left it.