Python Dictionary values() Method

Returns a list of values from a dictionary


The values() method returns a list of values from a dictionary.




Example: Print all values from the dictionary

D = {'name': 'Bob', 'age': 25}
L = D.values()
print(L)    # dict_values([25, 'Bob'])

values() method is generally used to iterate through all the values from a dictionary.

Example: Iterate through all the values from a dictionary

D = {'name': 'Bob', 'age': 25}
for x in D.values():
    print(x)    # 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.

Example: View object reflects any changes done to the dictionary

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)    # dict_values([25, 'xx'])