Dictionary Methods in Python

Python dictionaries come with a variety of built-in methods that allow you to interact with and manipulate the data stored within them. Understanding these methods is essential for effective dictionary usage.

1. clear()

The clear() method removes all items from the dictionary, leaving it empty.

Python
# Example
my_dict = {"name": "John", "age": 30, "city": "New York"}
my_dict.clear()
print(my_dict)  # Output: {}

2. copy()

The copy() method returns a shallow copy of the dictionary. This means that a new dictionary is created with the same keys and values as the original.

Python
# Example
my_dict = {"name": "John", "age": 30}
new_dict = my_dict.copy()
print(new_dict)  # Output: {"name": "John", "age": 30}

3. fromkeys()

The fromkeys() method creates a new dictionary from the given sequence of keys, with values set to a specified value (default is None).

Python
# Example
keys = ("a", "b", "c")
default_value = 0
my_dict = dict.fromkeys(keys, default_value)
print(my_dict)  # Output: {"a": 0, "b": 0, "c": 0}

4. get()

The get() method returns the value for the specified key if the key is in the dictionary. If not, it returns a default value (default is None).

Python
# Example
my_dict = {"name": "John", "age": 30}
print(my_dict.get("name"))  # Output: John
print(my_dict.get("address", "Not Found"))  # Output: Not Found

5. items()

The items() method returns a view object that displays a list of a dictionary's key-value tuple pairs.

Python
# Example
my_dict = {"name": "John", "age": 30}
print(my_dict.items())  # Output: dict_items([("name", "John"), ("age", 30)])

6. keys()

The keys() method returns a view object that displays a list of all the keys in the dictionary.

Python
# Example
my_dict = {"name": "John", "age": 30}
print(my_dict.keys())  # Output: dict_keys(["name", "age"])

7. pop()

The pop() method removes the item with the specified key and returns its value. If the key is not found, it raises a KeyError unless a default value is provided.

Python
# Example
my_dict = {"name": "John", "age": 30}
print(my_dict.pop("age"))  # Output: 30
print(my_dict)  # Output: {"name": "John"}

8. popitem()

The popitem() method removes and returns the last key-value pair as a tuple. In Python versions prior to 3.7, it removes an arbitrary key-value pair.

Python
# Example
my_dict = {"name": "John", "age": 30}
print(my_dict.popitem())  # Output: ("age", 30) or ("name", "John") depending on the order
print(my_dict)  # Output: {"name": "John"} or {"age": 30"} depending on the order

9. setdefault()

The setdefault() method returns the value of a key if it is in the dictionary. If not, it inserts the key with a specified value (default is None) and returns that value.

Python
# Example
my_dict = {"name": "John"}
print(my_dict.setdefault("age", 30))  # Output: 30
print(my_dict)  # Output: {"name": "John", "age": 30}

10. update()

The update() method updates the dictionary with elements from another dictionary object or from an iterable of key-value pairs. Existing keys will be updated with new values.

Python
# Example
my_dict = {"name": "John", "age": 30}
my_dict.update({"age": 31, "city": "New York"})
print(my_dict)  # Output: {"name": "John", "age": 31, "city": "New York"}

11. values()

The values() method returns a view object that displays a list of all the values in the dictionary.

Python
# Example
my_dict = {"name": "John", "age": 30}
print(my_dict.values())  # Output: dict_values(["John", 30])