Python Dictionary values()

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview:

value() function is an in-built function in Python, which returns a view object. That view object displays a list of all the values in the dictionary.

Syntax of value() function in Python

Syntax for using values() function is given below.

Parameters of value() function in Python

values() function doesn't require any paramter.

Return Values of value() function in Python

The values() function returns an object displaying a list of all values within a dictionary.

Example of value() function in Python

Now let's take an example to understand the usage of the value() function.

Code:

Output:

What is Dictionary values() Function in Python?

The values() function in Python is used to retrieve all of the values inside a dictionary. This function doesn't take any parameters and gives a list of values inside a dictionary. If a dictionary has no value, this function returns a null dictionary.

More Examples

Example 1: Getting all values from dictionary

In the code below, the dictionary i.e days is created in which keys are assigned with their corresponding values. So as to retrieve the list of all the values inside the dictionary the values() function is used.

Code:

Output:

So, finally, the list of all the values is printed as an output.

Example 2: Sorting a Dictionary by Value

In the code below, the dictionary i.e "Original_dict" is created with its keys and values pairs inside it. Here we need to sort the dictionary according to the values, not keys. So firstly, we will store the sorted list of values inside "sorted_values", then create a new dictionary that is "sorted_dict". Then we iterate over that sorted_values list and add key-value pair inside the sorted_dict according to the corresponding keys of those sorted values.

Code:

Output:

So, finally sorted dictionary is printed as an output.

Example 3: How values() function works when a Dictionary is modified

Suppose a dictionary "friends" is given, and we have stored a list of values of this dictionary in "values" using the values() function, and after that, a key is deleted. So the question here is, does that list of values get affected? So to understand that concept, the code is given below.

Code:

Output:

So now it is clear that the list of values gets affected when we modify the dictionary. This happens because the values function does not contain a list of values but rather a view of all dictionary values. So, when the list is changed, the changes are reflected in the view object itself, as illustrated in the program above.

Example 4: When we are given a Name and Salary, then using value() to return the total salary of all Employees

In the code below, a dictionary "salary" is given, which contains the name as key and the corresponding salaries as values. Now for getting the total sum of all salaries, the sum() function in python is used over the list of values that is "salaries_list".

Code:

Output:

So, finally, the sum of all the salaries is printed as an output.

Conclusion:

  • value() function is an in-built function in Python which returns a view object.
  • We can directly print all the values in the dictionary.
  • We can sort the dictionary according to the values present there.
  • Values list is also affected when the dictionary is altered.
  • We can use the "sum()" function to sum up all the values in the dictionary.

Read More: