Python Dictionaries
Imagine you have a phonebook.
If you want to find Aarav's phone number, you do not usually say:
"Give me the third number in the phonebook."
Instead, you search for:
Aarav
and find the associated phone number.
Python dictionaries work in a similar way.
They store information as:
Key → Value
What Is a Dictionary?
A Python dictionary, commonly called a dict, stores data using key-value pairs.
For example:
person = { "name": "Aarav", "age": 21, "city": "Pune"}Think of it as:
name → Aarav
age → 21
city → Pune
The left side is the key.
The right side is the value.
The Phonebook Analogy
Imagine a phonebook:
Name → Phone Number
Aarav → 9876543210
Rahul → 9123456780
Priya → 9988776655
The name is the key.
The phone number is the value.
In Python:
phonebook = { "Aarav": "9876543210", "Rahul": "9123456780", "Priya": "9988776655"}To find Aarav's number:
print(phonebook["Aarav"])Python looks for the key "Aarav" and gives you its associated value.
Dictionary Syntax
A dictionary uses curly braces in Python code:
person = { "name": "Aarav", "age": 21}Each pair has this structure:
key : value
Multiple pairs are separated by commas.
For example:
student = { "name": "Aarav", "age": 21, "course": "Python"}Here we have three key-value pairs.
Keys and Values
Look at:
student = { "name": "Aarav", "age": 21}The keys are:
name
age
The values are:
Aarav
21
The key tells Python what information you are looking for.
The value is the information associated with that key.
Dictionary vs List
A list uses numbered positions.
For example:
fruits = ["Apple", "Banana", "Mango"]You access values using indexes:
fruits[0]The list thinks:
0 → Apple
1 → Banana
2 → Mango
A dictionary uses meaningful keys:
fruits = { "first": "Apple", "second": "Banana", "third": "Mango"}Now you access values using keys:
fruits["first"]Think of the Difference This Way
List
Imagine a row of numbered lockers:
Locker 0 → Apple
Locker 1 → Banana
Locker 2 → Mango
You need the number.
Dictionary
Imagine labeled lockers:
Fruit A → Apple
Fruit B → Banana
Fruit C → Mango
You use the label.
The key difference is:
Lists are position-oriented. Dictionaries are key-oriented.
Accessing Values
Suppose:
student = { "name": "Aarav", "age": 21, "course": "Python"}You can access the name:
print(student["name"])Output:
Aarav
Age:
print(student["age"])Output:
21
Course:
print(student["course"])Output:
Python
Adding New Data
You can add a new key-value pair:
student["city"] = "Pune"Now the dictionary contains:
name → Aarav
age → 21
course → Python
city → Pune
Changing a Value
You can update an existing value.
For example:
student["age"] = 22Now:
age → 22
The key remains the same, but its value changes.
Checking Whether a Key Exists
You can use in to check whether a key exists.
For example:
student = { "name": "Aarav", "age": 21}if "name" in student: print("Name exists")Output:
Name exists
This is useful when you are not sure whether information exists in a dictionary.
Looping Through a Dictionary
You can loop through dictionary keys:
student = { "name": "Aarav", "age": 21, "course": "Python"}for key in student: print(key)Output:
name
age
course
You can also access the value using the key:
for key in student: print(key, student[key])Output:
name Aarav
age 21
course Python
Real-World Example
Imagine an employee record:
employee = { "name": "Amit", "department": "Engineering", "experience": 2, "remote": True}This is much more descriptive than storing unrelated values in a list.
You can ask:
print(employee["department"])and get:
Engineering
The key tells you exactly what the value means.
Dictionaries and Structured Information
Dictionaries are especially useful when you have multiple properties describing one thing.
For example, a product:
product = { "name": "Laptop", "price": 55000, "brand": "ExampleBrand", "in_stock": True}You can think of this as a digital information card:
Product
├── name
├── price
├── brand
└── in_stock
This makes dictionaries extremely useful in real-world programs.
Dictionary Mental Model
flowchart LR
A["Dictionary"] --> B["Key: name"]
B --> C["Value: Aarav"]
A --> D["Key: age"]
D --> E["Value: 21"]
A --> F["Key: city"]
F --> G["Value: Pune"]
Think of every key as a label pointing toward its associated value.
PyDictKeysObject[8]Slot Index = hash(key) % 8Why Must Dictionary Keys Be Immutable? Python uses the key's hash to find its exact bucket index. If you could use a mutable list as a key and then modified the list, its hash would change—meaning Python would search the wrong bucket and lose your data!
A Common Beginner Mistake
Remember the difference between:
student["name"]and:
student[0]The first asks for the value associated with the key "name".
The second asks for a key literally equal to 0.
Dictionaries are not accessed the same way as lists.
When Should You Use a Dictionary?
Use a dictionary when you want to associate one piece of information with another.
Examples:
Username → Email
Product ID → Product
Country → Capital
Student ID → Student details
Word → Meaning
The common pattern is:
Key → Value
Key Takeaways
- A dictionary stores data as key-value pairs.
- The key identifies the information.
- The value is the information associated with the key.
- Dictionaries are accessed using keys.
- Lists generally use numeric indexes.
- Dictionaries are excellent for representing structured information.
- You can add, update, and retrieve key-value pairs.
- The phonebook analogy is useful: name is the key, phone number is the value.
Beginner rule: If a list feels like "give me item number 3," a dictionary feels like "give me the value associated with this label."