The values method of Python dictionary class

Method Name:

values

Method Signature:

values()

Method Overview:

  • Returns the values of the dictionary as dict_values instance.
  • Contents of the dict_values changes when the dictionary is changed of a value.

Parameters:

None

Return Value:

A dictionary view of type dict_values.

Example:

# Example Python program that obtains
# the values of a dictionary as a dict_values

# Create a dictionary
famousConstants = {"pi":3.14,
                   "Avogadro Constant": "6.02214076 x 10^23 mol^-1",
                   "Speed of Light":"186000 miles per second",
                   "Boltzmann Constant": "1.380650 x 10^-23 joule per kelvin"
                  };

# Obtain a dictionary view of the values
onlyValues = famousConstants.values();
print(onlyValues);
print(type(onlyValues));

famousConstants["pi"] = 3.1415
# Add precision to pi
print("Values view after changing a ditionary value:");
print(onlyValues);

Output:

dict_values([3.14, '6.02214076 x 10^23 mol^-1', '186000 miles per second', '1.380650 x 10^-23 joule per kelvin'])
<class 'dict_values'>
Values view after changing a ditionary value:
dict_values([3.1415, '6.02214076 x 10^23 mol^-1', '186000 miles per second', '1.380650 x 10^-23 joule per kelvin'])

 


Copyright 2024 © pythontic.com