English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Python Functions

Python Data Types

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python dictionary values() usage and example

Python dictionary methods

The values() method returns a view object that displays a list of all values in the dictionary.

The syntax of values() is:

dictionary.values()

values() parameters

The values() method does not take any parameters.

values() return value

The values() method returns a view object that displays a list of all values in the given dictionary.

Example1:Get all values from the dictionary

# Dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
print(sales.values())

When running the program, the output is:

dict_values([2, 4, 3])

Example2:How does the values() work when modifying the dictionary?

# Dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
values = sales.values()
print('Original item:', values)
# Delete an item from the dictionary
del[sales['apple']]
print('Updated item after update:', values)

When running the program, the output is:

Original item: dict_values([2, 4, 3])
Updated item after update: dict_values([4, 3])

The view object values itself does not return a list of sales item values, but returns a view of all values in the dictionary.

If the list is updated, the changes will be reflected in the view object itself, as shown in the above program.

Python dictionary methods