5 4 1 Dictionary Operations Explained
Key Concepts
Dictionary operations in Python allow you to manipulate dictionaries, which are collections of key-value pairs. The key concepts include:
- Adding and Updating Items
- Accessing Items
- Removing Items
- Dictionary Methods
1. Adding and Updating Items
You can add new key-value pairs to a dictionary or update the value of an existing key. This is done using the assignment operator =
.
Example:
student = {"name": "Alice", "age": 25} # Adding a new key-value pair student["major"] = "Computer Science" print(student) # Output: {'name': 'Alice', 'age': 25, 'major': 'Computer Science'} # Updating an existing key's value student["age"] = 26 print(student) # Output: {'name': 'Alice', 'age': 26, 'major': 'Computer Science'}
Analogy: Think of a dictionary as a filing cabinet where each drawer (key) holds a specific document (value). You can add a new drawer or replace the document in an existing drawer.
2. Accessing Items
You can access the value associated with a specific key using the key inside square brackets []
or the get()
method.
Example:
student = {"name": "Alice", "age": 25} # Accessing using square brackets print(student["name"]) # Output: Alice # Accessing using the get() method print(student.get("age")) # Output: 25
Analogy: Think of a dictionary as a phone book where you look up a person's name (key) to find their phone number (value).
3. Removing Items
You can remove a key-value pair from a dictionary using the del
statement or the pop()
method.
Example:
student = {"name": "Alice", "age": 25, "major": "Computer Science"} # Removing using del statement del student["major"] print(student) # Output: {'name': 'Alice', 'age': 25} # Removing using pop() method age = student.pop("age") print(age) # Output: 25 print(student) # Output: {'name': 'Alice'}
Analogy: Think of a dictionary as a pantry where you can remove a specific item (key-value pair) when it is no longer needed.
4. Dictionary Methods
Python provides several built-in methods to manipulate dictionaries. Some common methods include keys()
, values()
, and items()
.
Example:
student = {"name": "Alice", "age": 25, "major": "Computer Science"} # Getting all keys print(student.keys()) # Output: dict_keys(['name', 'age', 'major']) # Getting all values print(student.values()) # Output: dict_values(['Alice', 25, 'Computer Science']) # Getting all key-value pairs print(student.items()) # Output: dict_items([('name', 'Alice'), ('age', 25), ('major', 'Computer Science')])
Analogy: Think of a dictionary as a library where you can list all the books (keys), their authors (values), or both (items).
Putting It All Together
By understanding and using these dictionary operations effectively, you can manipulate key-value pairs in Python. Dictionaries are particularly useful for storing and retrieving data based on unique keys.
Example:
student = {"name": "Alice", "age": 25} student["major"] = "Computer Science" print(student["name"]) # Output: Alice del student["age"] print(student.keys()) # Output: dict_keys(['name', 'major'])