{"id":3192,"date":"2021-04-21T12:44:59","date_gmt":"2021-04-21T07:14:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=3192"},"modified":"2021-11-22T18:44:58","modified_gmt":"2021-11-22T13:14:58","slug":"python-iterate-over-dictionary-all-key-value-pairs","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-iterate-over-dictionary-all-key-value-pairs\/","title":{"rendered":"Python: Iterate Over Dictionary (All Key-Value pairs)"},"content":{"rendered":"
Python’s implementation of an associative array, which is a data structure, is dictionaries. A dictionary is a collection of key-value pairs. Each key-value pair represents a key and its associated value.<\/p>\n
Enclosing a comma-separated list of key-value pairs in curly braces defines a dictionary { }. A colon \u2018 : \u2018<\/strong> separates each key from its associated value.<\/p>\n Note:<\/strong><\/p>\n The keys and values of a dictionary are iterated over in the same order as they were generated in Python 3.6 and later. However, this behaviour varies between Python versions and is dependent on the dictionary’s insertion and deletion history.<\/p>\n Examples:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to traverse the dictionary some of them are:<\/p>\n To iterate over all keys in a dictionary, a dictionary object may also be used as an iterable object. As a result, we can apply for loop on a dictionary with ease. It loops through all the keys in the dictionary by using for in the dictionary. We will pick the value associated with each key and print it.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n When dealing with dictionaries, you’ll almost certainly want to use both the keys and the values. .items(), a method that returns a new view of the dictionary’s items, is one of the most useful ways to iterate through a dictionary in Python.<\/p>\n This sequence is an iterable View object that contains all of the dictionary’s key,value elements. It is supported by the original dictionary. Let’s use this to iterate over all of the dictionary’s key-value pairs.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Since the items() function of a dictionary returns an iterable sequence of key-value pairs, we may use this list comprehension to iterate over all diction pairs.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n You can find yourself in a situation where you have an existing dictionary and want to construct a new one to store only the data that meets a set of criteria. This can be accomplished using an if statement inside a for loop, as shown below:<\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Python’s implementation of an associative array, which is a data structure, is dictionaries. A dictionary is a collection of key-value pairs. Each key-value pair represents a key and its associated value. Enclosing a comma-separated list of key-value pairs in curly braces defines a dictionary { }. A colon \u2018 : \u2018 separates each key from …<\/p>\n\n
dictionary = {'This': 100, 'is':200, 'BTechGeeks':300}<\/pre>\n
This 100\r\nis 200\r\nBTechGeeks 300<\/pre>\n
Traverse the dictionary<\/h2>\n
\n
Method #1: Using for loop<\/h3>\n
# Given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# Using for loop to traverse the dictionary\r\nfor key in dictionary:\r\n # here key gives the key of the dictionary\r\n # Getting value at key\r\n value = dictionary[key]\r\n # printing values and keys\r\n print(key, value)\r\n<\/pre>\n
This 100\r\nis 200\r\nBTechGeeks 300<\/pre>\n
Method #2:Using items()<\/h3>\n
# Given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# Using items and converting dictionary to list\r\ndictlist = list(dictionary.items())\r\n# Traverse the dictlist and print key and values of dictionary\r\nfor i in dictlist:\r\n # printing key and value\r\n print(i[0], i[1])\r\n<\/pre>\n
This 100\r\nis 200\r\nBTechGeeks 300<\/pre>\n
Method #3:Using List Comprehension<\/h3>\n
# Given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# Using list comprehension\r\n[print(key, value) for key, value in dictionary.items()]\r\n<\/pre>\n
This 100\r\nis 200\r\nBTechGeeks 300<\/pre>\n
Filtering Items in dictionary<\/h3>\n
# Given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# Create a new empty dictionary\r\nnewdictionary = dict()\r\n# Traverse the original dictionary and check the condition\r\nfor key, value in dictionary.items():\r\n # If value meets the criteria, it should be saved in new dict.\r\n if value <= 200:\r\n newdictionary[key] = value\r\n# printing the new dictionary\r\nprint(newdictionary)\r\n<\/pre>\n
{'This': 100, 'is': 200}<\/pre>\n
\n