Skip to content

Python values() function in Dictionary | Example code

  • by

Python values() function is used to get values of the dictionary. This method actually returns a view object that displays a list of all the values in the dictionary.

dictionary.values()

Example Python Dictionary values function

Simple example code.

fruits = {'apple': 120, 'orange': 30, 'grapes': 80}

print(fruits.values())

Output:

Python values() function in Dictionary

How values() works when a dictionary is modified?

Answer: When a value is changed or modified in the dictionary, the view object also gets updated:

fruits = {'apple': 120, 'orange': 30, 'grapes': 80}

values = fruits.values()

print('Original:', values)

# delete an item from dictionary
del[fruits['apple']]

print('Updated:', values)

Output:

Original: dict_values([120, 30, 80])
Updated: dict_values([30, 80])

Do comment if you have any doubts or suggestions on this Python dictionary function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading