Menu Close

Python Dictionary values Method

Python Dictionary values Method

In this guide, we are going to learn all about the Python dictionary values() method to return the list with all the keys of the Dictionary.
In the previous tutorial, we have learned the python dictionary update method to update the existing dictionary.

To understand this example, you should have basic knowledge of the Python dictionary.

Python Dictionary values Method

Python dictionary values method is a pre-defined dictionary method, that is used to return the list which contains all the values of the dictionary.

Syntax

The syntax of python dictionary values method is:-

dictionary.values()

Parameter

Dictionary values function in Python does not accept parameter.

Return Value

Dictionary values function in python return list object that contains only values of the dictionary.

Python dictionary values example

Here we will use the values method to return a view object. The view object contains the values of the dictionary, as a list.

Example 1:

Details = {'first_name': 'Vishvajit', 'last_name': 'Rao', 'roll_no': 120, 'course': 'BCA'}
result = Details.values()
print(result)

Output

dict_values(['Vishvajit', 'Rao', 120, 'BCA'])

Example 2:

You can use for loop to iterate the list returned by the dictionary values method.

Details = {'first_name': 'Vishvajit', 'last_name': 'Rao', 'roll_no': 120, 'course': 'BCA'}
result = Details.values()

for i in result:
	print(i)

Output

Vishvajit
Rao
120
BCA

Note:- Any changes made in the original dictionary, will reflect the returned list.

Example 3:

Details = {'first_name': 'Vishvajit', 'last_name': 'Rao', 'roll_no': 120, 'course': 'BCA'}
result = Details.values()
Details['address'] = 'Noida'

for i in result:
	print(i)

Conclusion

In this guide, you have seen all about the dictionary values method to get the list that contains all the values of the dictionary.
If this article helped you, please continue visiting for further interesting python tutorial.

Other Dictionary Methods


For more information:- Click Here

Python Dictionary copy Method
Python Dictionary update Method

Related Posts