In this Python tutorial, you will learn what values() method of dictionary dict class does, its syntax, and how to use this method to access all the values in the dictionary, with example programs.
Python Dictionary values()
Python Dictionary values() method returns a new view object containing dictionary’s values. This view object presents dynamic data, which means if any update happens to the dictionary, the view object reflects those changes.
The view object returned by values() is of type dict_values. View objects support iteration and membership checks. So, we can iterate over the values, and also check if a value is present or not using membership test.
Syntax
The syntax of dict.values() is
dict.values()
values() method returns object of type dict_values.
Examples (2)
1. Iterate over dictionary values using dict.values()
In this example, we will iterate over the dictionary values using dict.values() iterable.
Python Program
dictionary = {'a': 58, 'b': 61, 'c': 39}
for value in dictionary.values():
print(value)
Program Output
58
61
39
2. Check if a specific value is present in dictionary
In this example, we will use dict.values() to check if a specific value is present in dictionary or not.
Python Program
myDictionary = {'a': 58, 'b': 61, 'c': 39}
value = 39
if value in myDictionary.values():
print('Value present in dictionary.')
else:
print('Value not present in dictionary.')
Program Output
Value present in dictionary.
As the value is present in the dictionary, the expression value in dict.values() returned True.
Conclusion
In this Python Tutorial, we learned about Python Dictionary method dict.values().