In this tutorial, we’ll learn how to use Python dictionary values() method with the help of multiple Python code examples.
Outline
- Introduction: Python Dictionary values() Method
- Syntax of values() Method
- Example 1: Get All Values from Python Dictionary
- Example 2: values() Method Response to Updated Dictionary
- Example 3: values() Method Return all Values
- Conclusion
Introduction: Python Dictionary values() Method
This method is used to return a view object having a list of all the values(not keys) of specified Python dictionary.
Syntax of values() Method
dictionaryName.values()
- Python dictionary values method does not take any arguments.
- It returns a view object having a list of all the values(not keys) of dictionary.
Example 1: Get All Values from Python Dictionary
dictItems={'color':'green','number':2345,'place':'Havelian'} print(dictItems.values())
Output
dict_values(['green', 2345, 'Havelian'])
As you can see here, all the values(from key, value pairs of dictionary) are returned.
Example 2: values() Method Response to Updated Dictionary
dictItems={'color':'green','number':2345,'place':'Havelian'} values=dictItems.values() print(values) dictItems['nearBy']='Abbottabad' // add item to dictionary print(values)
Output
dict_values(['green', 2345, 'Havelian']) // before updating dictionary dict_values(['green', 2345, 'Havelian', 'Abbottabad']) // after updating dictionary
Any updates made to the list will be reflected on the view object(values) as well. Reason is that it does not return a list of items, but just returns a view of all the values of specified dictionary.
Example 3: values() Method Return all Values
dictItems={'color':'green','place':'Havelian'} returnedValue=dictItems.values() print(returnedValue)
Output
dict_values(['green', 'Havelian'])
As you can see that this method returned a view object having a list of all values(not keys) of the specified dictionary.
Conclusion
To conclude this tutorial, now you have an in-depth practical knowledge of how to use Python dictionary values method. I’ll be very happy to receive your valuable thoughts on this post. Thank you for reading it.
You may like to read:
How To Use Python Dictionary PopItem() Method – Easy Python Guide