Open In App

Python dictionary values()

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

values() method in Python is used to obtain a view object that contains all the values in a dictionary. This view object is dynamic, meaning it updates automatically if the dictionary is modified. If we use the type() method on the return value, we get "dict_values object". It must be cast to obtain the actual list. Example:

Python
d = {'A': 'Python', 'B': 'Java', 'C': 'C++'}

v = d.values()
print(v)

Output
dict_values(['Python', 'Java', 'C++'])

Explanation: values() method returns a dict_values view object containing all the values present in the dictionary d.

values() syntax

dict.values()

Here, dict is the dictionary from which the values are to be retrieved.

Parameters:

  • values() method does not take any parameters.

Returns: This method returns a dict_values view object, which behaves like a dynamic list of all the values in the dictionary. If the dictionary is updated, the view reflects these changes automatically.

Examples of values()

Example 1: Iterating over dictionary values

This code demonstrates how to iterate over the values of a dictionary using values().

Python
d = {'A': 'Python', 'B': 'Java', 'C': 'C++'}

for value in d.values():
    print(value)

Output
Python
Java
C++

Explanation: values() method returns a view object that contains all the values in the dictionary. The for loop then iterates through each value and prints it.

Example 2: Dynamic nature of values()

This code shows how the values() method reflects changes made to the dictionary dynamically.

Python
d = {'A': 'Python', 'B': 'Java'}

values = d.values()
d['C'] = 'C++'
print(values)

Output
dict_values(['Python', 'Java', 'C++'])

Explanation: When a new key-value pair is added to the dictionary, the values() view object updates automatically to include the new value 'C++'.

Example 3: Converting values() to a list

This code demonstrates how to convert dictionary values to a list.

Python
d = {'A': 'Python', 'B': 'Java', 'C': 'C++'}

values_list = list(d.values())
print(values_list)

Output
['Python', 'Java', 'C++']

Explanation: values() method returns a view of the dictionary’s values, and using list() converts this view into an actual list object containing the values.

Related Articles:


Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy