{"id":5902,"date":"2021-05-13T13:16:05","date_gmt":"2021-05-13T07:46:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=5902"},"modified":"2021-11-22T18:42:44","modified_gmt":"2021-11-22T13:12:44","slug":"loop-iterate-over-all-values-of-dictionary-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/loop-iterate-over-all-values-of-dictionary-in-python\/","title":{"rendered":"Loop \/ Iterate over all Values of Dictionary in Python"},"content":{"rendered":"
In Python, dictionaries are useful data structures that use keys for indexing. They are an unordered sequence of items (key-value pairs), so the order does not matter. The keys are not able to be changed. Dictionaries, like lists, can hold a variety of data types, including integers, floats, strings, NaN, Booleans, lists, arrays, and even nested dictionaries.<\/p>\n
Given a dictionary, the task is to iterate over all values of the dictionary .<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to traverse the values of dictionary some of them are:<\/p>\n To iterate over the keys of a dictionary, a dictionary object can also be used as an iterable object. We can easily iterate over all keys in the dictionary if we use it with a for loop. The value associated with that key can then be selected during iteration.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The dictionary class in Python has a function items() that returns an iterable sequence of all the dictionary’s key-value pairs, dict items. It shows all of the dictionary’s items (key-value pairs), so any changes to the original dictionary will be reflected in this sequence. We can’t use indexing with this sequence, either. This can be used in conjunction with a for loop to iterate over all pairs in the dictionary, and we can select the second element of the pair \/ tuple, i.e. the value, while iterating.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The dictionary class in Python has a function values() that returns an iterable sequence of all dictionary values, i.e. dict values. It is a view of the dictionary’s entire value set, which means that any changes to the original dictionary will be reflected in this sequence. We can’t use indexing with this sequence, either. However, we can use this in conjunction with a for loop to iterate through all of the dictionary’s values.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This list comprehension can also be used to iterate through all of the dictionary’s values and print each one separately.<\/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 Related Programs<\/strong>:<\/p>\n In Python, dictionaries are useful data structures that use keys for indexing. They are an unordered sequence of items (key-value pairs), so the order does not matter. The keys are not able to be changed. Dictionaries, like lists, can hold a variety of data types, including integers, floats, strings, NaN, Booleans, lists, arrays, and even …<\/p>\ndictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}<\/pre>\n
600\r\n300\r\n900<\/pre>\n
Traverse the values of Dictionary<\/h3>\n
\n
Method #1:Using for loop<\/h3>\n
# Given dictionary\r\ndictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}\r\n# using for loop to traverse the dictionary\r\nfor key in dictionary:\r\n print(dictionary[key])\r\n<\/pre>\n
600\r\n300\r\n900<\/pre>\n
Method #2:Using\u00a0 items()<\/h3>\n
# Given dictionary\r\ndictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}\r\n# using items()\r\nfor key, value in dictionary.items():\r\n # printing the value\r\n print(value)\r\n<\/pre>\n
600\r\n300\r\n900<\/pre>\n
Method #3:Using values()<\/h3>\n
# Given dictionary\r\ndictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}\r\n# using values()\r\nfor value in dictionary.values():\r\n # printing the value\r\n print(value)\r\n<\/pre>\n
600\r\n300\r\n900<\/pre>\n
Method #4:Using\u00a0 list Comprehension<\/h3>\n
# Given dictionary\r\ndictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}\r\n# using list comprehension\r\nprint([value for value in dictionary.values()])\r\n<\/pre>\n
[600, 300, 900]<\/pre>\n
Method #5:Converting values to list<\/h3>\n
# Given dictionary\r\ndictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}\r\n# converting given dictionary values to list using values() function\r\nlist_values = list(dictionary.values())\r\n# print the dictionary values\r\nfor value in list_values:\r\n print(value)\r\n<\/pre>\n
600\r\n300\r\n900<\/pre>\n
\n