Open In App

Python dictionary values()

Last Updated : 26 Feb, 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++'}

# using values() to get all values
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.

values() examples

Example 1: Iterating over dictionary values

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

# using values() to iterate over dictionary values
for value in d.values():
    print(value)

Output
Python
Java
C++

Explanation: values() method returns a view object that can be iterated over to access each value in the dictionary d .

Example 2: Dynamic nature of values()

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

# getting values
values = d.values()

# adding a new key-value pair
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

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

# converting values to a list
values_list = list(d.values())

print(values_list)

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

Explanation: we can convert the dict_values object into a list, allowing us to perform list operations such as indexing and sorting.


Next Article
Article Tags :
Practice Tags :

Similar Reads