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.

1. clear()

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

Tutorial – Python Dictionary clear()

Python Program

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

Output

{}

2. 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.

Tutorial – Python Dictionary copy()

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 Code Copy

Output

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

3. fromkeys()

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

Tutorial – Python Dictionary fromkeys()

Python Program

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

Output

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

4. get()

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

Tutorial – Python Dictionary get()

Python Program

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

Output

5

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.

Tutorial – Python Dictionary items()

Python Program

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

Output

a - 4
b - 5
c - 6

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.

Tutorial – Python Dictionary keys()

Python Program

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

Output

a
b
c

7. 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.

Tutorial – Python Dictionary values()

Python Program

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

Output

4
5
6

8. 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.

Tutorial – Python Dictionary pop()

Python Program

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

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.

9. popitem()

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

Tutorial – Python Dictionary popitem()

Python Program

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

Output

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

10. 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.

Tutorial – Python Dictionary setdefault()

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 Code Copy

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.

11. 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.

Tutorial – Python Dictionary update()

Python Program

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

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.

Related Tutorials

Code copied to clipboard successfully 👍