Creating & Accessing Dictionaries
Dictionaries store key-value pairs. They're unordered, mutable, and incredibly fast for lookups. Keys must be unique and immutable.
15 min•By Priygop Team•Updated 2026
Dictionaries
Dictionaries
# Creating dictionaries
person = {"name": "Alice", "age": 25, "city": "Mumbai"}
empty = {}
from_constructor = dict(name="Bob", age=30)
# Accessing values
print(person["name"]) # Alice
print(person.get("age")) # 25
print(person.get("email", "N/A")) # N/A (default)
# Adding/Modifying
person["email"] = "alice@email.com" # add new
person["age"] = 26 # modify existing
# Checking keys
print("name" in person) # True
print("phone" in person) # False
# Removing
del person["email"]
popped = person.pop("city", "Unknown")
print(f"Removed: {popped}")
print(person)Tip
Tip
Always use .get() instead of [] when a key might not exist. dict.get('key', default) prevents KeyError.
Diagram
Loading diagram…
Map for dictionaries. Object for records/config. WeakMap for metadata that shouldnt prevent GC.
Common Mistake
Warning
dict['missing_key'] crashes with KeyError. Use dict.get('missing_key', 'fallback') for safe access.
Practice Task
Note
(1) Create a person dict. (2) Access safely with .get(). (3) Add and remove keys. (4) Check if a key exists with 'in'.