Python Dictionary Methods

Python dict class provides many methods that transform or operate on the items (key:value pairs) of the given dictionary.

In this tutorial, we will go through the methods of a Python Dictionary with examples.

The following are the individual tutorials for each of the dictionary class methods.

  • dict.clear()Python Tutorial for dictionary clear() method to remove all the items from the given dictionary.
  • dict.copy()Python Tutorial for dictionary copy() method to make a shallow copy of the given dictionary.
  • dict.get()Python Tutorial for dictionary get() method to get the value for a specified key from the given dictionary.
  • dict.items()Python Tutorial for dictionary items() method to get a view object for the items in the given dictionary.
  • dict.keys()Python Tutorial for dictionary keys() method to get a view object for the keys in the given dictionary.
  • dict.values()Python Tutorial for dictionary values() method to get a view object for the values in the given dictionary.
  • dict.fromkeys()Python Tutorial for dictionary fromkeys() method to create a dictionary with given collection of keys, and a default value.
  • dict.pop()Python Tutorial for dictionary pop() method to remove the entry with the specified key, from the given dictionary.
  • dict.popitem()Python Tutorial for dictionary popitem() method to remove an entry and return it, from the given dictionary.
  • dict.setdefault()Python Tutorial for dictionary setdefault() method to get the value for specified key from the dictionary, if the key is present. If the key is not present, then the method returns specified default value.
  • dict.update()Python Tutorial for dictionary update() method to update the values in the dictionary with the matching keys of an other dictionary.

Examples

dict.clear()

In the following program, we will create a dictionary with a few key:value pairs and call clear() method on the dictionary. The items in the dictionary should be removed.

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}
dictionary.clear()
print('Output :',dictionary)
Try Online

Output

Output : {}

dict.copy()

In the following program, we will initialize a dictionary with a few key:value pairs and make a copy of this dictionary. After that we shall make some modifications to the copy and print the two dictionaries.

example.py

dictionary1 = {'a': 54, 'b': 87, 'c': 61}
dictionary2 = dictionary1.copy()
dictionary2['b'] = 11
print("dictionary1 :", dictionary1)
print("dictionary2 :", dictionary2)
Try Online

Output

dictionary1 : {'a': 54, 'b': 87, 'c': 61}
dictionary2 : {'a': 54, 'b': 11, 'c': 61}

dict.get(key[, default])

In the following program, we will take a dictionary and get the value for key 'b'.

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}
value = dictionary.get('b')
print(f'b : {value}')
Try Online

Output

b : 87

dict.items()

In the following program, we will take a dictionary and iterate over the dictionary items using the view object dict.items().

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}
print(dictionary.items())

for key, value in dictionary.items():
    print(key, '-', value)
Try Online

Output

dict_items([('a', 54), ('b', 87), ('c', 61)])
a - 54
b - 87
c - 61

dict.keys()

In the following program, we will take a dictionary and iterate over the dictionary keys using the view object dict.keys().

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}
print(dictionary.keys())

for key in dictionary.keys():
    print(key)
Try Online

Output

dict_keys(['a', 'b', 'c'])
a
b
c

dict.values()

In the following program, we will take a dictionary and iterate over the dictionary values using the view object dict.values().

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}
print(dictionary.values())

for value in dictionary.values():
    print(value)
Try Online

Output

dict_values([54, 87, 61])
54
87
61

dict.fromkeys(sequence[, value])

In the following program, we will create a dictionary from a list of strings as keys, and 0 as the default value for each of the keys.

example.py

dictionary = dict.fromkeys(['a', 'b', 'c'], 0)
print(dictionary)
Try Online

Output

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

dict.pop(key[, default])

In the following program, we will take a dictionary and pop() key 'b' from the dictionary.

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}
value = dictionary.pop('b')
print(f'popped value is {value}')
Try Online

Output

popped value is 87

dict.popitem()

In the following program, we will take a dictionary and popitem() method iteratively on a dictionary.

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}

while dictionary:
    print(dictionary.popitem())
Try Online

Output

('c', 61)
('b', 87)
('a', 54)

popitem() method returns key:value pairs in Last-In-First-Out order.

dict.setdefault(key[, default])

In the following program, we will take a dictionary and call setdefault() method with a key present in the dictionary.

example.py

dictionary = {'a': 54, 'b': 87, 'c': 61}
value = dictionary.setdefault('b')
print(f'b : {value}')
Try Online

Output

b : 87

dict.update([other])

In the following program, we will take a dictionary and update its values from the items of another dictionary.

example.py

dictionary1 = {'a': 54, 'b': 87, 'c': 61}
dictionary2 = {'a': 77, 'b': 44, 'p': 1}
dictionary1.update(dictionary2)
print(dictionary1)
Try Online

Output

{'a': 77, 'b': 44, 'c': 61, 'p': 1}

Conclusion

In this Python Tutorial, we learned about Python Dictionary Methods and their usage with explanation and example programs.