Python Dictionary values() Method

Python Dictionary Values() Method

This is a detailed tutorial of the Python Dictionary values() Method. Learn to get the list of values (as a view object) present in a given dictionary.

Python Dictionary values()

The method dict.values() is used to get the view object that contains the list of all of the values of a given dictionary. This method fetches the values from all of the key, value pair items in the dictionary and show them as a collection object known as the dict_values object.

Syntax

The basic syntax of the values() method is given below.

values() Parameters

This method does not take any arguments.

values() Return Value

It returns the view object that further contains the list of all of the values contained in the dictionary on which this method is applied. This view object is labeled as dict_values.

Examples

Given below are the two examples illustrating the usage of the dict.values() method.

Example 1. Fetching values form a given Dictionary using the method values()

Here we have got a dictionary languages and we’re simply applying the method values() on it to get the view object containing the dictionary values.


#A Dictionary of languages
languages = {
0 : "English",
1 : "Hindi",
2 : "Punjabi",
3 : "Spanish",
4 : "French"
}


#Getting only Dictionary Values using values()
values = languages.values()


print("Values:", values)


Output.

Example 2. dict_values gets updated if the dictionary is updated

This example illustrates that if the changes will be made to the dictionary even after applying this method, these changes will be automatically reflected into the values view object, dict_values.


#A Dictionary of languages
languages = {
0 : "English",
1 : "Hindi",
2 : "Punjabi",
3 : "Spanish",
4 : "French"
}


#Getting only Dictionary Values using values()
values = languages.values()


print("Values Before Adding More Items:", values)


#Adding more Dictionary items
languages[5] = "Tamil"
languages[6] = "Telegu"


print("Values After Adding More Items:", values)


Initially, the dictionary was having 5 items. So, this method returned a dict_values object with 5 values. But later on, we’ve added two more items into the dictionary and hence the dict_values object also gets updated with the other two newly added items.

Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *