{"id":5051,"date":"2023-10-26T15:29:52","date_gmt":"2023-10-26T09:59:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=5051"},"modified":"2023-11-10T12:00:47","modified_gmt":"2023-11-10T06:30:47","slug":"python-print-all-values-of-a-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-print-all-values-of-a-dictionary\/","title":{"rendered":"Python: Print all Values of a Dictionary"},"content":{"rendered":"
Dictionaries are Python’s implementation of an associative list, which is a data structure. A dictionary is a collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.<\/p>\n
Given a dictionary, the task is to print all the values of the dictionary.<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to display all the values of the dictionary some of them are:<\/p>\n The dictionary class in Python has a function dict.values() that returns an iterable sequence of dictionary values. We can iterate over the sequence of values returned by the function values() using a for loop, and we can print each value while iterating.<\/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. Print the list(values)<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can also use this list comprehension to iterate through all of the dictionary’s values and print each one individually.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries are Python’s implementation of an associative list, which is a data structure. A dictionary is a collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair. Given a dictionary, the task is to print all the values of the dictionary. Examples: Input: dictionary = {‘this’: …<\/p>\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}<\/pre>\n
200\r\n100\r\n300<\/pre>\n
Display all values of the Dictionary<\/h2>\n
\n
Method #1:Using for loop and values() function<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# Traverse the dictionary using for loop\r\nfor value in dictionary.values():\r\n # print the values\r\n print(value)\r\n<\/pre>\n
200\r\n100\r\n300<\/pre>\n
Method #2:Converting values to list<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# converting values to list\r\nvalueslist = list(dictionary.values())\r\n# print the values list\r\nfor value in valueslist:\r\n print(value)\r\n<\/pre>\n
200\r\n100\r\n300<\/pre>\n
Method #3:Using\u00a0 list Comprehension<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# using list comprehension\r\nprint([value for value in dictionary.values()])\r\n<\/pre>\n
[200, 100, 300]<\/pre>\n
\n