Returns a list of values from a dictionary
Usage
The values()
method returns a list of values from a dictionary.
Syntax
dictionary.values()
Examples
# Print all values from the dictionary
D = {'name': 'Bob', 'age': 25}
L = D.values()
print(L)
# Prints dict_values([25, 'Bob'])
values()
method is generally used to iterate through all the values from a dictionary.
# Iterate through all the values from a dictionary
D = {'name': 'Bob', 'age': 25}
for x in D.values():
print(x)
# Prints 25 Bob
values() Returns View Object
The object returned by items()
is a view object. It provides a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.
D = {'name': 'Bob', 'age': 25}
# Assign dict values to L
L = D.values()
# modify dict D
D['name'] = 'xx'
# L reflects changes done to dict D
print(L)
# Prints dict_values([25, 'xx'])