Free cookie consent management tool by TermsFeed The values method of Python dictionary class | Pythontic.com

The values method of Python dictionary class

Method Name:

values

Method Signature:

values()

Method Overview:

  • Using a Python dictionary data can be organised using keys. The data stored against a key is called a value. The dict class of Python organises key-value pairs as a dictionary. The keys in a Python dictionary are unique. A dictionary can not have two keys with the same value.
  • A dynamic view of the dictionary values can be obtained by calling the dict method values().
  • The dict method values() returns the values of the dictionary as dict_values instance.
  • Contents of the dict_values changes when the dictionary is changed of a value.

Values of a Python dictionary as a dict_view object

Parameters:

None

Return Value:

A dictionary view of type dict_values.

Example:

# Example Python program that obtains a view
# of the values from a Python dictionary
d1 = {"x":27, "y":-2, "z":5}
vals = d1.values()
print("Values present in the dictionary:")
print(type(vals))
print(vals)

# Change the value of y
d1["y"] = -4

# Witness the dictionary changes from the view
print("After changes in values of the dictionary:")
print(vals)

Output:

Values present in the dictionary:
<class 'dict_values'>
dict_values([27, -2, 5])
After changes in values of the dictionary:
dict_values([27, -4, 5])

 


Copyright 2025 © pythontic.com