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 Set update()
Python Program to Convert Two Lists Into a Dictionary
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Function Arguments
Python Set union()
Python Program to Convert Kilometers to Miles
Python any()
Python Data Types
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Find ASCII Value of Character
Python Program to Count the Number of Each Vowel
Python format()
Python hash()
Python Program to Shuffle Deck of Cards
Python Keywords and Identifiers
Python break and continue
Python Program to Find Sum of Natural Numbers Using Recursion
Python Global, Local and Nonlocal variables
Python Program to Remove Duplicate Element From a List
Python Set isdisjoint()
Python filter()
Python Program Read a File Line by Line Into a List
Python Program to Generate a Random Number
Python id()
Python Program to Get the File Name From the File Path
Machine Learning with Python for everyone - Mark E.Fenner
Python object()
Python Program to Display the multiplication Table
Python property()
Python Program to Swap Two Variables
Python String join()
Python Program to Print all Prime Numbers in an Interval