Open In App

Python dictionary values()

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

values() is an inbuilt method in Python programming language that returns a view object. The view object contains the values of the dictionary, as a list. If you use the type() method on the return value, you get “dict_values object”. It must be cast to obtain the actual list.

Python Dictionary values() Method Syntax

Syntax: dictionary_name.values()

Parameters: There are no parameters 

Returns:  A list of all the values available in a given dictionary. The values have been stored in a reversed manner.

Get all values from the dictionary

In the first part we are extracting all the numerical values from the dictionary and in the second part we are extracting the string values using the Python value() function.

Python3




# numerical values
dictionary = {"raj": 2, "striver": 3, "vikram": 4}
print(dictionary.values())
 
 
# string values
dictionary = {"geeks": "5", "for": "3", "Geeks": "5"}
print(dictionary.values())


Output:

dict_values([2, 3, 4])
dict_values(['5', '3', '5'])

Get a sum of all values from the dictionary

Program for illustration of values() method in finding the total salary. Given name and salary, return the total salary of all employees using the Python sum() function.

Python3




# stores name and corresponding salaries
salary = {"raj" : 50000, "striver" : 60000, "vikram" : 5000}
 
# stores the salaries only
list1 = salary.values()
print(sum(list1)) # prints the sum of all salaries


Output:

115000

Last Updated : 10 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads