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 bin()
Python Program to Create a Long Multiline String
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Check Whether a String is Palindrome or Not
Python Program to Remove Punctuations From a String
Python String upper()
Python Set add()
Python String format()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python hasattr()
Python List Comprehension
Python Program to Differentiate Between type() and isinstance()
Python round()
Python Tuple count()
How to get current date and time in Python?
Python String casefold()
Python Tuple index()
Python Matrices and NumPy Arrays
Python Dictionary setdefault()
Python Dictionary pop()
Python Program to Check the File Size
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python sorted()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python String lower()
Python Dictionary
Python if...else Statement
Python Set update()
Python all()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python next()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...