{"id":25044,"date":"2021-11-10T09:37:10","date_gmt":"2021-11-10T04:07:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=25044"},"modified":"2021-11-10T09:37:10","modified_gmt":"2021-11-10T04:07:10","slug":"python-dictionary-values-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-dictionary-values-function-with-examples\/","title":{"rendered":"Python Dictionary values() Function with Examples"},"content":{"rendered":"
Dictionary values() Function in Python:<\/strong><\/p>\n The values() method returns a view object that displays a list of all the dictionary’s values.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong>\u00a0This method doesn\u2019t accept any parameters<\/p>\n Return Value:<\/strong><\/p>\n The values() method returns a view object containing a list of all values in a given dictionary.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dictionary values() Function in Python: The values() method returns a view object that displays a list of all the dictionary’s values. Syntax: dictionary.values() Parameters:\u00a0This method doesn\u2019t accept any parameters Return Value: The values() method returns a view object containing a list of all values in a given dictionary. Examples: Example1: Input: Given dictionary = {20: …<\/p>\ndictionary.values()<\/pre>\n
Given dictionary = {20: 'good', 30: 'morning', 40: 'btechgeeks'}<\/pre>\n
The values of the given dictionary is:\r\ndict_values(['good', 'morning', 'btechgeeks'])<\/pre>\n
Given dictionary = {'hello': 1, 'this': 3, 'is': 4, 'btechgeeks': 8}<\/pre>\n
The values of the given dictionary is:\r\ndict_values([1, 3, 4, 8])<\/pre>\n
Dictionary values() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {20: 'good', 30: 'morning', 40: 'btechgeeks'}\r\n# Get all the values of the given dictionary using the values() method.\r\n# Store it in another variable.\r\ndict_valus = gvn_dict.values()\r\n# Print all the values of the given dictionary.\r\nprint(\"The values of the given dictionary is:\")\r\nprint(dict_valus)\r\n<\/pre>\n
The values of the given dictionary is:\r\ndict_values(['good', 'morning', 'btechgeeks'])<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dict = {}\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dict[keyy] = valuee\r\n\r\n# Get all the values of the given dictionary using the values() method.\r\n# Store it in another variable.\r\ndict_valus = gvn_dict.values()\r\n# Print all the values of the given dictionary.\r\nprint(\"The values of the given dictionary is:\")\r\nprint(dict_valus)\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 4\r\nEnter key and value separated by spaces = hello 350\r\nEnter key and value separated by spaces = this 450\r\nEnter key and value separated by spaces = is 550\r\nEnter key and value separated by spaces = btechgeeks 650\r\nThe values of the given dictionary is:\r\ndict_values(['350', '450', '550', '650'])<\/pre>\n","protected":false},"excerpt":{"rendered":"