Python 3 – dictionary values() Method
In this article, we will take a detailed look at the Python 3 dictionary values() method. The values() method is used to return a list of all the values in the dictionary.
The syntax of the values() method is as follows:
dictionary_name.values()
Here is an example of how to use the values() method:
# creating a dictionary
my_dict = {'a': 1, 'b': 2, 'c':3}
# accessing the values of the dictionary
my_values = my_dict.values()
# printing the values of the dictionary
print(my_values)
Output:
dict_values([1, 2, 3])
The output shows that the values() method returns a dict_values object containing the values of the dictionary.
Parameters
The values() method takes no parameters and returns a list of all the values in the dictionary.
Example
Here is an example of how to use the values() method in Python 3:
# creating a dictionary
my_dict = {'apple': 5, 'banana': 6, 'orange': 8}
# accessing the values of the dictionary
my_values = my_dict.values()
# printing the values of the dictionary
print(my_values)
Output:
dict_values([5, 6, 8])
The output shows that the values() method returns a dict_values object containing the values of the dictionary.
Working with the values() Method
The values() method returns a view object that contains the values in the dictionary. This view object can be used to perform various operations on the values of the dictionary.
Accessing Values
You can access the values in the dictionary using the values() method. This is demonstrated in the example below:
# create a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# get the values of the dictionary
my_values = my_dict.values()
# print the values of the dictionary
print(my_values)
Output:
dict_values([1, 2, 3])
Finding the Length of the Values List
You can use the len() function to find the length of the values list. This is demonstrated in the code below:
# create a dictionary
my_dict = {'apple': 5, 'banana': 6, 'orange': 8}
# get the values of the dictionary
my_values = my_dict.values()
# get the length of the values list
length = len(my_values)
# print the length of the values list
print(length)
Output:
3
Converting the Values List to a Set
You can use the set() function to convert the values list to a set. This eliminates any duplicates in the values list. This is demonstrated in the code below:
# create a dictionary
my_dict = {'apple': 5, 'banana': 6, 'orange': 8}
# get the values of the dictionary
my_values = my_dict.values()
# convert the values list into a set
my_set = set(my_values)
# print the set
print(my_set)
Output:
{8, 5, 6}
Removing Values from the Dictionary
You can remove values from the dictionary using the values() method. This is demonstrated in the code below:
# create a dictionary
my_dict = {'apple': 5, 'banana': 6, 'orange': 8}
# get the values of the dictionary
my_values = my_dict.values()
# remove the value '6' from the dictionary
my_dict.popitem()
# print the updated dictionary
print(my_dict)
Output:
{'apple': 5, 'banana': 6}
Conclusion
In conclusion, the Python 3 dictionary values() method is used to return a list of all the values in a dictionary. This method does not take any parameters and returns a view object that can be used to perform various operations on the values of the dictionary. We hope that this article has provided a comprehensive understanding of the values() method in Python 3 dictionaries.