Python Dictionary values()

The dict.values() method returns the dictionary view object that provides a dynamic view of all the values in the dictionary. This view object changes when the dictionary changes.

Syntax:

dict.values()

Parameters:

No parameters

Return Value:

Returns a view object with the list of all values in the dictionary.

The following example demonstrates the dict.values() method.

Example: Get All Values
romanNums = {'I':1,'II':2,'III':3,'IV':4,'V':5}
values = romanNums.values()
print(values)
Output
dict_values([1, 2, 3, 4, 5])

The following example shows the working of dict.values() when the dictionary is updated.

Example: values()
romanNums = {'I':1,'II':2,'III':3,'IV':4,'V':5}
values = romanNums.values()
print("Dictionary Values: ",values)
romanNums['VI'] = 6
print("After updating the dictionary: ",values)
Output
Dictionary Values:  dict_values([1, 2, 3, 4, 5])
After updating the dictionary:  dict_values([1, 2, 3, 4, 5, 6])

In the above example, the values() function does not return the list of values but returns a view object of the values. Any change in the dictionary is reflected in the view object.

Want to check how much you know Python?