{"id":3705,"date":"2021-04-25T19:29:01","date_gmt":"2021-04-25T13:59:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=3705"},"modified":"2021-11-22T18:43:08","modified_gmt":"2021-11-22T13:13:08","slug":"python-dictionary-values-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-dictionary-values-function\/","title":{"rendered":"Python Dictionary: Values() Function"},"content":{"rendered":"
Python’s implementation of an associative array data structure is dictionaries. A dictionary is a collection of key\/value pairs. A key pair and its associated value are used to represent each key pair.<\/p>\n
A dictionary is a list of key-value pairs enclosed in curly braces and separated by commas. The column \u2018:’ separates the value of each key.<\/p>\n
Sorting a dictionary solely for the purpose of obtaining a representation of the sorted dictionary is not possible. By default, dictionary entries are ordered, but other data types, such as lists and tuples, are not. As a result, you’ll need an ordered data form, such as a list\u2014probably a list of tuples.<\/p>\n
values() is a built-in Python method that returns a list of all the values available in a given dictionary.<\/p>\n
Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n There is no need to pass parameters.<\/p>\n Return type:<\/strong><\/p>\n It returns a sequence containing a view of all dictionary values. Because the sequence is just a view of values, any change to a value in the dictionary will be reflected in the sequence as well.<\/p>\n We can display all the values of the dictionary by using the given syntax above.<\/p>\n Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If we first fetch all of the dictionary’s values with the values() function and then modify the dictionary, the changes will be reflected in the sequence of the previously fetched values as well.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The sequence returned by the values() function can be passed to the list to create a list of all values in the dictionary .<\/p>\n We use list() function to achieve this<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can display maximum and minimum values by using max() and min() functions .<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can display sum of dictionary values by using sum() function .<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can display average of dictionary values with the help of sum() and len() functions.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Python’s implementation of an associative array data structure is dictionaries. A dictionary is a collection of key\/value pairs. A key pair and its associated value are used to represent each key pair. A dictionary is a list of key-value pairs enclosed in curly braces and separated by commas. The column \u2018:’ separates the value of …<\/p>\ndictionary_name.values()<\/code><\/p>\n
1)Display all the values of the dictionary<\/h3>\n
# given dictionary\r\ndictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}\r\n# getting all the values from the given dictionary\r\ndictvalues = dictionary.values()\r\n# printing the values\r\nprint(dictvalues)\r\n<\/pre>\n
dict_values([700, 200, 100, 300])<\/pre>\n
2)Update\/Modify the values of the dictionary<\/h3>\n
# given dictionary\r\ndictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}\r\n# getting all the values from the given dictionary\r\ndictvalues = dictionary.values()\r\n# print the values before modification\r\nprint(\"Before Modification\", dictvalues)\r\n# updating value of BTechGeeks to 1000\r\ndictionary['BTechGeeks'] = 1000\r\n# printing the after modification\r\nprint(\"After Modification\", dictvalues)\r\n<\/pre>\n
Before Modification dict_values([700, 200, 100, 300])\r\nAfter Modification dict_values([700, 200, 100, 1000])<\/pre>\n
3)Conversion of dictionary values to list<\/h3>\n
# given dictionary\r\ndictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}\r\n# getting all the values from the given dictionary and converting to list\r\ndictvalues = list(dictionary.values())\r\n# printing the dictionary values list\r\nprint(dictvalues)\r\n<\/pre>\n
[700, 200, 100, 300]<\/pre>\n
4)Display Maximum and minimum values<\/h3>\n
# given dictionary\r\ndictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}\r\n# getting all the values from the given dictionary and converting to list\r\ndictvalues = dictionary.values()\r\n# Getting max and min values\r\nmaxvalue = max(dictvalues)\r\nminvalue = min(dictvalues)\r\n# print the max value\r\nprint(\"Maximum value in dictionary\", maxvalue)\r\nprint(\"Minimum value in dictionary\", minvalue)\r\n<\/pre>\n
Maximum value in dictionary 700\r\nMinimum value in dictionary 100<\/pre>\n
5)Display sum of dictionary values<\/h3>\n
# given dictionary\r\ndictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}\r\n# getting all the values from the given dictionary and converting to list\r\ndictvalues = dictionary.values()\r\n# Getting sum of dictionary values\r\nvaluesum = sum(dictvalues)\r\n# print sum of values\r\nprint(\"Sum of dictionary values\", valuesum)\r\n<\/pre>\n
Sum of dictionary values 1300<\/pre>\n
6)Display Average of dictionary values<\/h3>\n
# given dictionary\r\ndictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}\r\n# getting all the values from the given dictionary and converting to list\r\ndictvalues = dictionary.values()\r\n# Getting average of dictionary values\r\naveragevalue = sum(dictvalues)\/len(dictvalues)\r\n# print average of values\r\nprint(\"Average of dictionary values\", averagevalue)\r\n<\/pre>\n
Average of dictionary values 325.0<\/pre>\n
\n