Python Basics · Lesson 9
Dictionaries
Learn how Python dictionaries store information using key-value pairs and how to access, add, update, remove and process data.
What you will learn
- • Key-value pairs
- • Creating dictionaries
- • Accessing values
- • Adding data
- • Updating values
- • get()
- • keys()
- • values()
- • items()
- • pop()
- • Membership testing
- • Nested dictionaries
Part 1
What is a dictionary?
A dictionary stores data as key-value pairs. Instead of accessing information using numeric positions like a list, you normally access dictionary values using their keys.
student = {
"name": "Aman",
"age": 21,
"course": "Python"
}
print(student)CHECK YOUR UNDERSTANDING
What type of structure is a Python dictionary?
Complete the empty dictionary: student = _____
Keys and values
Each dictionary entry contains a key and a value. The key is used to identify the associated value.
student = {
"name": "Aman",
"age": 21
}CHECK YOUR UNDERSTANDING
In {"name": "Aman"}, what is the key?
CHECK YOUR UNDERSTANDING
In {"age": 21}, what is the value?
Accessing dictionary values
You can access a value by placing its key inside square brackets.
student = {
"name": "Aman",
"age": 21,
"course": "Python"
}
print(student["name"])CHECK YOUR UNDERSTANDING
What does student["name"] return?
Access the course: student["_____"]
Important
Updating a dictionary value
Assign a new value to an existing key to update the dictionary.
student = {
"name": "Aman",
"age": 21
}
student["age"] = 22
print(student)CHECK YOUR UNDERSTANDING
What is the new value of student['age']?
Adding a new key-value pair
Assigning a value to a key that does not already exist adds a new entry.
student = {
"name": "Aman",
"age": 21
}
student["city"] = "Dehradun"
print(student)CHECK YOUR UNDERSTANDING
What happens when student['city'] is assigned?
Add an email: student["_____"] = "aman@example.com"
get()
The get() method retrieves a value using a key. It can also provide a default value when the key does not exist.
student = {
"name": "Aman",
"age": 21
}
print(student.get("name"))CHECK YOUR UNDERSTANDING
What does student.get("name") return?
print(student.get("city", "Unknown"))CHECK YOUR UNDERSTANDING
If city does not exist, what will student.get("city", "Unknown") return?
Get a value safely: student.get("city", "Unknown")
keys()
The keys() method provides the dictionary keys.
student = {
"name": "Aman",
"age": 21,
"course": "Python"
}
print(student.keys())CHECK YOUR UNDERSTANDING
What does keys() provide?
Complete: student._____() to get the keys
values()
The values() method provides the values stored in a dictionary.
student = {
"name": "Aman",
"age": 21,
"course": "Python"
}
print(student.values())CHECK YOUR UNDERSTANDING
What does values() provide?
Complete: student._____() to get the values
items()
The items() method provides dictionary entries as key-value pairs.
student = {
"name": "Aman",
"age": 21
}
print(student.items())CHECK YOUR UNDERSTANDING
What does items() provide?
Complete: student._____() to access key-value pairs
pop()
Dictionary pop() removes an entry using its key and returns its value.
student = {
"name": "Aman",
"age": 21,
"course": "Python"
}
removed = student.pop("age")
print(removed)
print(student)CHECK YOUR UNDERSTANDING
What value is stored in removed?
CHECK YOUR UNDERSTANDING
Which key is removed by student.pop('age')?
Remove a key: student.pop("age")
Checking whether a key exists
The in operator can check whether a key exists in a dictionary.
student = {
"name": "Aman",
"age": 21
}
print("name" in student)CHECK YOUR UNDERSTANDING
What does "name" in student return?
CHECK YOUR UNDERSTANDING
What does "city" in student return if city is not a key?
Predict the output
Think before running the code
product = {
"name": "Laptop",
"price": 55000
}
product["price"] = 60000
print(product["price"])CHECK YOUR UNDERSTANDING
What will the program print?
Next level
Nested dictionaries
A dictionary can contain another dictionary as a value. This is useful for representing structured data.
student = {
"name": "Aman",
"details": {
"age": 21,
"city": "Dehradun"
}
}
print(student["details"]["city"])CHECK YOUR UNDERSTANDING
What will student["details"]["city"] return?
Access the nested age value: student["details"]["_____"]
Debug the dictionary
student = {
"name": "Aman",
"age": 21
}
print(student["city"])CHECK YOUR UNDERSTANDING
Why can this code cause a KeyError?
print(student.get("city", "Not available"))Data Analytics Connection
Dictionaries and structured data
Dictionaries are useful for representing records where each field has a meaningful name.
sale = {
"product": "Laptop",
"quantity": 3,
"price": 55000
}
revenue = sale["quantity"] * sale["price"]
print(revenue)CHECK YOUR UNDERSTANDING
What is the revenue in this example?
Practice
Complete the dictionary operations
Get a value safely: student.get("city", "Unknown")
Get all keys: student._____()
Get all values: student._____()
Get key-value pairs: student._____()
Remove a key: student.pop("age")
Mini Challenge
Build a student record
Create a dictionary containing a student's name, course and city. Add an age, update the city and then print the student's details.
student = {
"name": "Priya",
"course": "Data Analytics",
"city": "Dehradun"
}
student["age"] = 22
student["city"] = "Delhi"
print(student)Experiment by adding your own fields.
Final Challenge
Build a sales record
Create a dictionary containing a product, price and quantity. Calculate revenue from the dictionary values. Then add a category field and display all the keys.
sale = {
"product": "Laptop",
"price": 55000,
"quantity": 2
}
revenue = sale["price"] * sale["quantity"]
sale["category"] = "Electronics"
print(revenue)
print(sale.keys())Expected revenue: ₹110000
Lesson recap
- ✓ Dictionaries store key-value pairs.
- ✓ Keys are used to access values.
- ✓ New keys can be added by assignment.
- ✓ Existing values can be updated.
- ✓ get() safely retrieves values.
- ✓ keys() provides dictionary keys.
- ✓ values() provides dictionary values.
- ✓ items() provides key-value pairs.
- ✓ pop() removes an entry using its key.
- ✓ in can check whether a key exists.
- ✓ Dictionaries can contain nested dictionaries.
- ✓ Dictionaries are useful for structured records.