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:
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Swap Two Variables
Python Set union()
Python Program to Differentiate Between type() and isinstance()
Python ascii()
Python dir()
Python String maketrans()
Python vars()
Python Program to Sort a Dictionary by Value
Python locals()
Python enumerate()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python exec()
Python delattr()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Display the multiplication Table
Python open()
Python filter()
Python Set clear()
Python sleep()
Python Program to Capitalize the First Character of a String
Python String casefold()
Python Program to Safely Create a Nested Directory
Python Program to Check Armstrong Number
Python Program to Remove Duplicate Element From a List
Python Set remove()
Python Set issubset()
Python Program to Split a List Into Evenly Sized Chunks
Python String rindex()
Python divmod()
Python Program to Parse a String to a Float or Int
Node.js vs Python for Backend Development