Concept: Dictionary methods
A dictionary has several methods that can be used to manipulate its contents. Some of the most commonly used methods are:
get(): Returns the value for a specified key. If the key does not exist, it returns a default value (orNoneif no default is provided).user = {"firstName": "Alice", "lastName": "Healy", "age": 30, "username": "alice_h"} print(user.get("username")) # Output: alice_h print(user.get("gender", "Not specified")) # Output: Not specifiedkeys(): Returns a view object that displays a list of all the keys in the dictionary.user = {"firstName": "Alice", "lastName": "Healy", "age": 30, "username": "alice_h"} print(user.keys()) # Output: dict_keys(['firstName', 'lastName', 'age', 'username'])values(): Returns a view object that displays a list of all the values in the dictionary.user = {"firstName": "Alice", "lastName": "Healy", "age": 30, "username": "alice_h"} print(user.values()) # Output: dict_values(['Alice', 'Healy', 30, 'alice_h'])items(): Returns a view object that displays a list of all the key-value pairs in the dictionary. Those pairs are represented as 2-tuples or pairs.user = {"firstName": "Alice", "lastName": "Healy", "age": 30, "username": "alice_h"} print(user.items()) # Output: dict_items([('firstName', 'Alice'), ('lastName', 'Healy'), ('age', 30), ('username', 'alice_h')])pop(): Removes the specified key and returns the corresponding value. If the key does not exist, it raises aKeyError.user = {"firstName": "Alice", "lastName": "Healy", "age": 30, "username": "alice_h"} removed_value = user.pop("age") print(removed_value) # Output: 30 print(user) # Output: {'firstName': 'Alice', 'lastName': 'Healy', 'username': 'alice_h'}update(): Updates the dictionary with key-value pairs from another dictionary or an iterable of key-value pairs. If a key already exists, its value will be updated.user = {"firstName": "Alice", "lastName": "Healy", "age": 30, "username": "alice_h"} user.update({"age": 31, "gender": "female"}) print(user) # Output: {'firstName': 'Alice', 'lastName': 'Healy', 'age': 31, 'username': 'alice_h', 'gender': 'female'}clear(): Removes all items from the dictionary.user = {"firstName": "Alice", "lastName": "Healy", "age": 30, "username": "alice_h"} user.clear() print(user) # Output: {}