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 Strings
Python Program to Sort a Dictionary by Value
Python String strip()
Python Program to Print Hello world!
Python Program to Iterate Through Two Lists in Parallel
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Slice Lists
Python len()
Python Set issuperset()
Python Program to Illustrate Different Set Operations
Python String count()
Python Program to Find the Largest Among Three Numbers
Python Type Conversion and Type Casting
Python Machine Learning - Sebastian Raschka
Python Tuple
Python Keywords and Identifiers
Python Dictionary get()
Python float()
Python Program to Display the multiplication Table
APIs in Node.js vs Python - A Comparison
Python Program to Convert Two Lists Into a Dictionary
Deep Learning in Python - LazyProgrammer
Python Get Current time
Python File I/O Operation
Python Program to Find the Sum of Natural Numbers
Python delattr()
Python Directory and Files Management
Python Closures
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python compile()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python List append()