The values()
method is a built-in dictionary method in Python that returns a view object which displays a list of all the values in the dictionary. This method allows you to access and work with the values stored in the dictionary.
Parameter Values
This function does not accept any parameters.Return Values
The values()
method returns a view object containing the dictionary's values.
How to Use values()
in Python
Example 1:
The values()
method returns a view object that displays a list of all the values of the dictionary.
data = {'a': 1, 'b': 2, 'c': 3}
print(list(data.values()))
Example 2:
The view object is dynamic, and any changes to the dictionary will be reflected in the values list.
data = {'a': 1, 'b': 2, 'c': 3}
vals = data.values()
data['a'] = 100
print(list(vals))
Example 3:
The values are not a copy of the values stored in the dictionary, but rather a view of them.
data = {'a': 1, 'b': 2, 'c': 3}
vals = data.values()
data['d'] = 4
print(list(vals))