In this Python Dictionary tutorial, you will learn about methods available in Dictionary dict class, with examples.

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

Examples

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

Python Program

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

Output

Output : {}

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

Python Program

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}

3. dict.get(key[, default])

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

Python Program

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

Output

b : 87

4. dict.items()

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

Python Program

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

5. dict.keys()

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

Python Program

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

6. dict.values()

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

Python Program

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

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

Python Program

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

Output

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

8. dict.pop(key[, default])

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

Python Program

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

Output

popped value is 87

9. dict.popitem()

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

Python Program

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.

10. dict.setdefault(key[, default])

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

Python Program

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

Output

b : 87

11. dict.update([other])

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

Python Program

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.