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
1 2 3 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 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
1 2 3 4 5 |
Dictionary: {'grapes': 5, 'orange': 'mango', 'banana': 'apple'} The value method returns: dict_values([5, 'mango', 'apple']) |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 |
# 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
1 2 3 4 |
Original items: dict_values([3, 2, 4]) Updated items: dict_values([3, 4]) |