Python Dictionary values() method
Python Dictionary values() method
The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary.
Syntax
dictionary.values()
Parameter
NA
Return
This method returns a view object. The view object contains the values of the dictionary as a list.
Example 1
# Python program representing
# the dictionary.values() method
# initializing the dictionary
fruits = {
"banana": "apple",
"orange": "mango",
"grapes": 5
}
# printing the Dictionary
print("Dictionary:",fruits)
# returns a view object that displays a list of all the values
Values= fruits.values()
print("The value method returns: \n",Values)
Output
Dictionary: {'grapes': 5, 'orange': 'mango', 'banana': 'apple'}
The value method returns:
dict_values([5, 'mango', 'apple'])
Example 2
# Python program representing
# the dictionary.values() method
# initializing a random dictionary
dictionary = { 'apple': 2, 'orange': 3, 'grapes': 4 }
# returning a view object
Value = dictionary.values()
print('Original items:', Value)
# delete an item from dictionary
del[dictionary['apple']]
print('Updated items:', Value)
Output
Original items: dict_values([3, 2, 4]) Updated items: dict_values([3, 4])
