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 enumerate()
Python Functions
Python Set isdisjoint()
Python Program to Count the Number of Occurrence of a Character in String
Python String lower()
Python Program to Check Leap Year
Python Program to Make a Flattened List from Nested List
Python Program to Swap Two Variables
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python String center()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Set copy()
Python Function Arguments
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Find HCF or GCD
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python max()
Python Program to Find the Size (Resolution) of a Image
Python bool()
Python List insert()
Python Recursion
Python strftime()
Python Program to Get the Class Name of an Instance
Python Dictionary fromkeys()
Python hash()
Python Global Keyword
Python Program to Get the Last Element of the List
Python List Comprehension
Python String rpartition()
Python RegEx
Python Program to Slice Lists
Python setattr()