Discover the most important Python dictionary methods with clear explanations and real-world examples. Learn how to use get, keys, values, items, update, pop, setdefault, copy, and more to master dictionary operations in Python. Perfect for beginners and students.
get(key[, default])
Purpose: Safely retrieve a value for a given key without raising an error if the key doesn’t exist.
Example:
user = {'name': 'Alice', 'age': 30, 'email': 'alice@example.com'}
# Get existing key
print(user.get('name')) # Output: Alice
# Get non-existent key
print(user.get('address')) # Output: None
print(user.get('address', 'Not provided')) # Output: Not provided
Real-world use: Looking up product prices in an e-commerce system where some products might not have prices defined.
keys()
Purpose: Get all keys in the dictionary.
Example:
car = {'make': 'Toyota', 'model': 'Camry', 'year': 2022}
print(car.keys()) # Output: dict_keys(['make', 'model', 'year'])
Real-world use: Getting all field names from a database record before processing them.
values()
Purpose: Get all values in the dictionary.
Example:
grades = {'math': 90, 'science': 85, 'history': 88}
print(grades.values()) # Output: dict_values([90, 85, 88])
Real-world use: Calculating the average of all values in a gradebook or survey results.
items()
Purpose: Get all key-value pairs as tuples.
Example:
inventory = {'apples': 10, 'bananas': 5, 'oranges': 8}
for item, quantity in inventory.items():
print(f"We have {quantity} {item}")
Real-world use: Displaying all items in a shopping cart with their quantities.
update([other])
Purpose: Update the dictionary with key-value pairs from another dictionary.
Example:
profile = {'name': 'Bob', 'age': 25}
additional_info = {'occupation': 'developer', 'city': 'New York'}
profile.update(additional_info)
print(profile)
# Output: {'name': 'Bob', 'age': 25, 'occupation': 'developer', 'city': 'New York'}
Real-world use: Merging user profile information from multiple sources.
pop(key[, default])
Purpose: Remove and return the value for a key.
Example:
config = {'theme': 'dark', 'font_size': 14, 'language': 'en'}
removed_value = config.pop('theme')
print(removed_value) # Output: dark
print(config) # Output: {'font_size': 14, 'language': 'en'}
Real-world use: Removing and processing items from a task queue.
popitem()
Purpose: Remove and return the last inserted key-value pair.
Example:
settings = {'volume': 80, 'brightness': 70, 'contrast': 50}
last_setting = settings.popitem()
print(last_setting) # Output: ('contrast', 50)
Real-world use: Processing items in LIFO (last-in, first-out) order.
clear()
Purpose: Remove all items from the dictionary.
Example:
shopping_cart = {'shirt': 2, 'pants': 1, 'shoes': 1}
shopping_cart.clear()
print(shopping_cart) # Output: {}
Real-world use: Emptying a shopping cart after checkout.
setdefault(key[, default])
Purpose: Get a value for a key, setting it to a default if the key doesn’t exist.
Example:
employee = {'name': 'John', 'position': 'manager'}
salary = employee.setdefault('salary', 50000)
print(salary) # Output: 50000
print(employee) # Output: {'name': 'John', 'position': 'manager', 'salary': 50000}
Real-world use: Initializing default values in configuration settings.
copy()
Purpose: Create a shallow copy of the dictionary.
Example:
original = {'a': 1, 'b': 2, 'c': 3}
duplicate = original.copy()
duplicate['d'] = 4
print(original) # Output: {'a': 1, 'b': 2, 'c': 3}
print(duplicate) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Real-world use: Creating a backup of configuration before making changes.
Here are three beginner-friendly tasks to practice working with Python dictionaries, along with sample solutions:
Objective: Create a program that stores student names and their grades, then calculates the average grade.
Requirements:
Example Output:
Students: {'Alice': 85, 'Bob': 90, 'Charlie': 78}
Average grade: 84.33
Objective: Build a simple inventory system that tracks product stock and allows restocking.
Requirements:
Example Output:
Current Inventory: {'apples': 10, 'bananas': 3, 'oranges': 7}
Warning: bananas stock is low (3 left)!
Objective: Create a simple contact book where users can add, search, and delete contacts.
Requirements:
{name: phone_number}
.Example Output:
Contacts:
- Alice: 123-456-7890
- Bob: 987-654-3210
Search for 'Alice': 123-456-7890
Tutorials, Roadmaps, Bootcamps & Visualization Projects