
Python's values() Method
The power of Python lies in its vast array of built-in methods, one of which is the values() method. This particular method presents a straightforward way to access the values that dwell within a dictionary.
Let's illustrate this with the syntax:
object.values()
Imagine that we start by constructing a dictionary, a simple one containing three keys.
dictionary = {"a": 1, "b": 2, "c": 3}
The next step involves employing the values() method to draw out the values from the dictionary.
We'll allocate these to a variable, aptly named "values".
values = dictionary.values()
After this, we can reveal the contents of the "values" variable with a print statement.
print(values)
This action crafts a dict_values object, a container that envelops all the values present in the dictionary.
dict_values([1, 2, 3])
One crucial point to note is the sequence of the returned values by values(). It mirrors precisely the arrangement of the keys in the dictionary.
However, if your preference leans towards working with lists, the dict_values object can readily be converted.
values = list(dictionary.values())
To round off, we can once again print the "values" variable.
print(values)
On doing this, the assemblage of values is returned in the highly versatile list format:
[1, 2, 3]