Table of Contents
In this tutorial, we will learn about the Python Dictionary values() method with the help of examples.
The values() method returns a view object that displays a list of all the values in the dictionary.
Example
marks = {'Physics':67, 'Maths':87}
print(marks.values())
# Output: dict_values([67, 87])
1. Syntax of Dictionary values()
The syntax of values() is:
dictionary.values()
2. values() Parameters
values() method doesn’t take any parameters.
3. Return value from values()
values() method returns a view object that displays a list of all values in a given dictionary.
4. Example 1: Get all values from the dictionary
# random sales dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
print(sales.values())
Output
dict_values([2, 4, 3])
5. Example 2: How values() works when a dictionary is modified?
# random sales dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
values = sales.values()
print('Original items:', values)
# delete an item from dictionary
del[sales['apple']]
print('Updated items:', values)
Output
Original items: dict_values([2, 4, 3]) Updated items: dict_values([4, 3])
The view object values doesn’t itself return a list of sales item values but it returns a view of all values of the dictionary.
If the list is updated at any time, the changes are reflected on the view object itself, as shown in the above program.
Related posts:
Python issubclass()
Python bytes()
Python Program to Find the Sum of Natural Numbers
Python Program to Create a Countdown Timer
Python Program to Get Line Count of a File
Python staticmethod()
Python break and continue
Python String replace()
Python Set issuperset()
Python List pop()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python tuple()
Python pass statement
Python Program to Print Colored Text to the Terminal
Python String translate()
Python reversed()
Python Dictionary fromkeys()
Python timestamp to datetime and vice-versa
Python strptime()
Python delattr()
Python String title()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python String rpartition()
Python Program to Add Two Matrices
Python Program to Find the Factors of a Number
Python String rindex()
Python Program to Trim Whitespace From a String
Python float()
Python Deep Learning Cookbook - Indra den Bakker
Python compile()
Python enumerate()