Python Dictionary Methods

Python Dictionary Methods

Python Dictionary class provides a set of builtin methods that we can call on dictionaries.

In this tutorial, we will learn about all of the Python Dictionary methods with description for each of them, and a well detailed example.

clear()

Dictionary clear() method deletes all the key:value pairs from the dictionary. In other words, it makes the dictionary empty.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
dictionary.clear()
print(dictionary)
Run this program

Output

{}

copy()

Dictionary copy() method returns a copy of the original dictionary. Any changes made to the items in the copied dictionary does not affect the original dictionary.

In the following example, we will copy dictionary to dictionary_1. And we will change the contents of dictionary_1. We shall print both dictionary and dictionary_1 proving that the update to copy of the dictionary does not affect the original dictionary.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
dictionary_1 = dictionary.copy()
dictionary_1["b"] = 2
print(dictionary)
print(dictionary_1)
Run this program

Output

{'a': 4, 'b': 5, 'c': 6}
{'a': 4, 'b': 2, 'c': 6}

fromkeys()

Dictionary fromkeys() method creates a new dictionary from the keys of given dictionary and an optional default value for the key-value pairs.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
dictionary_1 = dict.fromkeys(dictionary, 1)
print(dictionary_1)
Run this program

Output

{'a': 1, 'b': 1, 'c': 1}

get()

Dictionary get() method returns the value for the specified key. The key is specified by passing as argument to get() method.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
x = dictionary.get("b")
print(x)
Run this program

Output

5

items()

Dictionary items() method returns an iterator of type dict_items. You can iterate over each of the key, value in the dictionary. Also, the dict_items type support dynamic update to the dictionary.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
for key, value in dictionary.items():
    print(key, '-', value)
Run this program

Output

a - 4
b - 5
c - 6

keys()

Dictionary keys() method returns an iterator of type dict_keys. You can iterate over each of the key in the dictionary. Also, the dict_keys type support dynamic update to the dictionary.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
for key in dictionary.keys():
    print(key)
Run this program

Output

a
b
c

values()

Dictionary values() method returns an iterator of type dict_values. You can iterate over each of the value in the dictionary. Also, the dict_values type support dynamic update to the dictionary.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
for value in dictionary.values():
    print(value)
Run this program

Output

4
5
6

pop()

Dictionary pop() method removes the key-value pair of specified key, and returns just the value. The key is passed as argument to pop() method.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
x = dictionary.pop("b")
print(x)
print(dictionary)
Run this program

Output

5
{'a': 4, 'c': 6}

If you do not pass any key to the pop() method, it throws TypeError.

If a key that is not present is passed, pop() throws KeyError.

So, before popping a key-value pair, you may have to check if the key is present in the dictionary.

popitem()

Dictionary popitem() method removes the last inserted key-value pair of specified key, and returns this key-value pair.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
x = dictionary.popitem()
print(x)
print(dictionary)
Run this program

Output

('c', 6)
{'a': 4, 'b': 5}

setdefault()

Dictionary setdefault() method returns the value of the specified key if the key is present. If the key is not present, setdefault() method inserts a key-value pair with the default value and returns the default value.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
x = dictionary.setdefault("b")
print(x)
y = dictionary.setdefault("m", 0)
print(y)
print(dictionary)
Run this program

Output

5
0
{'a': 4, 'b': 5, 'c': 6, 'm': 0}

The key b is present, and therefore its value is returned. But key m is not present, therefore, the key-value pair m:0 is inserted into the dictionary and the value is returned.

update()

Dictionary update() method updates the key-value pairs of this dictionary with the key-value pairs from the dictionary passed as argument to update() method. Values for the keys that are present are updated, and the for the keys that are not present, those key-value pairs are inserted.

Python Program

dictionary = {"a": 4, "b": 5, "c": 6}
dictionary_1 = {"a": 8, "m": 2, "v": 7}
dictionary.update(dictionary_1)
print(dictionary)
Run this program

Output

{'a': 8, 'b': 5, 'c': 6, 'm': 2, 'v': 7}

Summary

In this tutorial of Python Examples, we learned all Dictionary methods, and went through examples for each of them.