Python dictionary methods are built-in functions that allow us to perform various operations on dictionaries, such as adding, updating, accessing and removing key-value pairs.
clear()
Removes all key-value pairs from the dictionary.
Syntax: dictionary_name.clear()
In the code below, we will remove all elements from a dictionary.
d = {"name": "Emma", "age": 25}
d.clear()
print(d)
Output
{}
copy()
Returns a shallow copy of the dictionary.
Syntax: dictionary_name.copy()
In the code below, we will create a copy of a dictionary.
d = {"name": "David", "age": 25}
new_d = d.copy()
print(new_d)
Output
{'name': 'David', 'age': 25}
get()
Returns the value associated with the specified key. If the key is not found, it returns None (or a default value if provided).
Syntax: dictionary_name.get(key, default_value)
In the code below, we will retrieve values using the get() method.
d = {"name": "Harry", "age": 25}
print(d.get("name"))
print(d.get("city"))
Output
Harry None
items()
Returns a view object containing all key-value pairs as tuples.
Syntax: dictionary_name.items()
In the code below, we will display all key-value pairs.
d = {"name": "Tupac", "age": 25}
print(list(d.items()))
Output
[('name', 'Tupac'), ('age', 25)]
keys()
Returns a view object containing all dictionary keys.
Syntax: dictionary_name.keys()
In the code below, we will display all dictionary keys.
d = {"name": "Alice", "age": 25}
print(list(d.keys()))
Output
['name', 'age']
values()
Returns a view object containing all dictionary values.
Syntax: dictionary_name.values()
In the code below, we will display all dictionary values.
d = {"name": "Joe", "age": 25}
print(list(d.values()))
Output
['Joe', 25]
update()
Updates the dictionary using another dictionary or iterable of key-value pairs.
Syntax: dictionary_name.update(other_dictionary)
In the code below, we will update one dictionary using another dictionary.
d = {"name": "Kate", "age": 25}
d.update({"age": 26, "city": "NY"})
print(d)
Output
{'name': 'Kate', 'age': 26, 'city': 'NY'}
pop()
Removes the specified key and returns its value.
Syntax: dictionary_name.pop(key)
In the code below, we will remove a key from the dictionary.
d = {"name": "Olive", "age": 25}
print(d.pop("age"))
print(d)
Output
25
{'name': 'Olive'}
popitem()
Removes and returns the last inserted key-value pair.
Syntax: dictionary_name.popitem()
In the code below, we will remove the last inserted item.
d = {"name": "Larry", "age": 25, "city": "Delhi"}
print(d.popitem())
print(d)
Output
('city', 'Delhi')
{'name': 'Larry', 'age': 25}
setdefault()
Returns the value of a key if it exists. Otherwise, inserts the key with the specified default value.
Syntax: dictionary_name.setdefault(key, default_value)
In the code below, we will insert a new key only if it does not already exist.
d = {"name": "Nate"}
d.setdefault("age", 25)
print(d)
Output
{'name': 'Nate', 'age': 25}
fromkeys()
Creates a new dictionary using the given iterable as keys and assigns the same default value to all keys.
Syntax: dict.fromkeys(iterable, value)
In the code below, we will create a dictionary from a list of keys.
keys = ["name", "age", "city"]
d = dict.fromkeys(keys, "Not Available")
print(d)
Output
{'name': 'Not Available', 'age': 'Not Available', 'city': 'Not Available'}