{"id":6511,"date":"2025-01-09T07:36:39","date_gmt":"2025-01-09T07:36:39","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=6511"},"modified":"2025-05-20T04:33:20","modified_gmt":"2025-05-20T04:33:20","slug":"how-to-iterate-over-dictionaries-using-for-loops","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/","title":{"rendered":"How to Iterate Over Dictionaries Using For Loops"},"content":{"rendered":"\n<p>Python Dictionaries allow you to store large amounts of data in a compact manner. Software developers heavily rely on dictionaries to store data in key-value format. Often they need to loop over dictionaries to access their keys, values or both keys &amp; values. This is commonly required to access, modify, filter or manipulate data. Sometimes it is also required to <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-copy-data-from-one-dictionary-to-another-in-python\/\">copy data from one dictionary to another<\/a>. There are several ways to loop over Python dictionaries. In this article, we will learn the most popular and powerful ways to iterate over dictionaries.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#How_to_Iterate_Over_Dictionaries_Using_For_Loops\" >How to Iterate Over Dictionaries Using For Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#1_Iterate_Over_Keys_Only\" >1. Iterate Over Keys Only<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#2_Iterate_Over_Values_Only\" >2. Iterate Over Values Only<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#3_Iterate_Over_Keys_Values\" >3. Iterate Over Keys &amp; Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#4_Iterate_over_Sorted_Keys\" >4. Iterate over Sorted Keys<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#5_Iterate_in_Reverse_Order\" >5. Iterate in Reverse Order<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#6_Iterate_over_Multiple_dictionaries\" >6. Iterate over Multiple dictionaries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#7_Using_map_function\" >7. Using map() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#8_Using_filter_function\" >8. Using filter() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Iterate_Over_Dictionaries_Using_For_Loops\"><\/span>How to Iterate Over Dictionaries Using For Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the different ways to iterate over Python dictionaries. Let us say you have the following Python dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {'a':1, 'b':2, 'c':3}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Iterate_Over_Keys_Only\"><\/span>1. Iterate Over Keys Only<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Since every dictionary item consists of key-value pairs, you can iterate over keys only, values only or both keys and values. In this case, you iterate over the keys of your dictionary only. This is the default way to iterate over a dictionary using for loop. For example, when you use the following code, Python will use the key values in each iteration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in data:<br>    print i<\/pre>\n\n\n\n<p>Here is the output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a<br>b<br>c<\/pre>\n\n\n\n<p>When you run for loop over dictionary this way, it automatically executes the .__iter__() builtin function available for each dictionary.<\/p>\n\n\n\n<p>This is very convenient since you can use the key to get the value in each iteration, as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in data:<br>    print data[i]<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1<br>2<br>3<\/pre>\n\n\n\n<p>You can also use this approach to get both key as well as value in each iteration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in data:<br>    print(i, data[i])<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a 1<br>b 2<br>c 3<\/pre>\n\n\n\n<p>Python also provides several ready made methods that directly return a list of all keys in dictionary. You can loop through this list to iterate over the keys. keys() is one such function. Here is an example of a list of keys of dictionary data, where each item of list is a string.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data.keys()<br>['a', 'b', 'c']<\/pre>\n\n\n\n<p>Here is an example to iterate over this list of dictionary keys.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in data.keys():<br>    print i<\/pre>\n\n\n\n<p>Here is the output. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'a'<br>'b'<br>'c'<\/pre>\n\n\n\n<p>If you have a very large dictionary with many keys, you can use iterkeys() function which returns an iterator to the list of keys, instead of returning an entire list of keys that is loaded into the system memory. You can iterate over dictionary keys using this iterator.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in data.iterkeys():<br>    print i<br>    <\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'a'<br>'b'<br>'c'<\/pre>\n\n\n\n<p>Please note, iterkeys() function may not be available in older Python versions such as Python &lt;=2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Iterate_Over_Values_Only\"><\/span>2. Iterate Over Values Only<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>On the other hand, if you want to access only the values of dictionary, or you want to loop over only the values of Python dictionary, then you can use functions such as values() and itervalues() for this purpose. They are built-in functions available by default for every dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data.values()<br>[1, 2, 3]<br><br>&gt;&gt;&gt; data.itervalues()<br>&lt;dictionary-valueiterator object at 0x0333F750&gt;<\/pre>\n\n\n\n<p>You can loop through either of them to get all dictionary values. Here is an example to loop through the result returned by values() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in data.values():<br>        print i<br>1<br>2<br>3<\/pre>\n\n\n\n<p>Here is an example to use itervalues().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in data.itervalues():<br>        print i<br>1<br>2<br>3<\/pre>\n\n\n\n<p>The main difference between using values() and itervalues() is that values() function returns a list of all dictionary values whereas itervalues() returns an iterator to the list of values. Since iterators do not occupy much memory, it is very useful if you have a large dictionary with many key-value pairs. On the other hand, the list returned by values() will be loaded into memory. So, itervalues() function offers better performance than values() function.<\/p>\n\n\n\n<p>In the previous method, we saw that if you have the key of each item in each iteration, you can easily get its value also. But in this case, if you have the value, you cannot retrieve the key. <\/p>\n\n\n\n<p>Please note, itervalues() function may not be available in older Python versions such as Python 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Iterate_Over_Keys_Values\"><\/span>3. Iterate Over Keys &amp; Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes, you may need to iterate over both key as well as values in a Python dictionary. For this purpose, you can use items() or iteritems() function. items() function will return a list of tuples, where each tuple consists of key and value as 2 items of the tuple. iteritems() returns an iterator to the list of such tuples. Every time you access the iterator, it will return a tuple consisting of key and value of next item in dictionary. Both these functions are available by default for every dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data.items()\n[('a', 1), ('c', 3), ('b', 2)]\n\n&gt;&gt;&gt; data.iteritems()\n&lt;dictionary-itemiterator object at 0x0333F750&gt;<\/pre>\n\n\n\n<p>You can loop through this list to get key-value combinations in each iteration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i, j in data.items():<br>    print i, j<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a 1<br>b 2<br>c 3<\/pre>\n\n\n\n<p>Here is an example to loop through keys and values using iteritems().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i, j in data.iteritems():<br>        print i, j<br>a 1<br>b 2<br>c 3<\/pre>\n\n\n\n<p>Here again the key difference between iteritems() and items() is that iteritems() returns an iterator to a dictionary, which is memory efficient whereas items() returns list of tuples which is completely loaded into memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Iterate_over_Sorted_Keys\"><\/span>4. Iterate over Sorted Keys<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So far we have seen how to iterate though dictionary in whatever order the items are present. In Python &lt;=3.6, dictionary items are stored in no particular order whereas in Python &gt;=3.7, they are stored in the same order as they are inserted. Nevertheless, sometimes Python developers need to iterate over the dictionary items in sorted order. You can do this by calling sorted() function on the dictionary.<\/p>\n\n\n\n<p>When you call sorted() function on an iterable such as dictionary, it will return a sorted list of dictionary keys.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; sorted(data)<br>['a', 'b', 'c']<\/pre>\n\n\n\n<p>You can iterate over this list to get a sorted order of keys. In each iteration, you can use these keys to get their corresponding values too.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in sorted(data):<br>        print i, data[i]<br><br>a 1<br>b 2<br>c 3<\/pre>\n\n\n\n<p>Please note, in this case, we have directly called sorted() function on the dictionary instead of its keys. If you called sorted on its list of keys, still you will get the same result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in sorted(<strong>data.keys()<\/strong>):<br>        print i, data[i]<br>a 1<br>b 2<br>c 3<\/pre>\n\n\n\n<p>On the other hand, if you pass a list of dictionary values to sorted() function, using data.values(), then sorted() function will return a list of sorted values. You can iterate over this list to loop over sorted values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in sorted(<strong>data.values()<\/strong>):<br>        print i<br>1<br>2<br>3<\/pre>\n\n\n\n<p>Sometimes, you may want to iterate through the sorted list of values, and get its key as well as value in each iteration. In this case, we will need to use the key argument of sorted function to specify that the sort key is value. We do this by creating a lambda function which simply returns the value of each tuple.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i, j in sorted(data.items(),key=lambda item:item[1]):<br>        print i, j<br><br>a 1<br>b 2<br>c 3<\/pre>\n\n\n\n<p>In other words, the lambda function tells sorted() function to sort result of data.items() by second item of each tuple, that is item[1].<\/p>\n\n\n\n<p>This is a very simple way to control the order of iteration of dictionary items.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Iterate_in_Reverse_Order\"><\/span>5. Iterate in Reverse Order<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>On the other hand, sometimes you may need to traverse the items in reverse order. In this case, you need to use reverse=true argument in sorted function. Again, here also you can choose to sort keys or values, depending on your requirement. We will look at each of these cases one by one. Here is an example to reverse sort and iterate through the keys of a dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in sorted(data,reverse=True):<br>        print i<br><br>c<br>b<br>a<\/pre>\n\n\n\n<p>Here is an example to not only reverse sort dictionary keys but also access the keys and corresponding values in each iteration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in sorted(data,reverse=True):<br>        print i, data[i]<br><br>c 3<br>b 2<br>a 1<\/pre>\n\n\n\n<p>If you only want to reverse sort and iterate through the dictionary values then you can pass data.values() in sorted function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in sorted(data.values(),reverse=True):<br>        print i<br><br>3<br>2<br>1<\/pre>\n\n\n\n<p>Sometimes, you may want to reverse sort a dictionary&#8217;s values, and display both keys and values together, in each iteration. In this case, you pass data.items() to sorted function. data.items() returns a list of 2-item tuples where each tuple consists of key and value. We also set reverse=True to reverse sort the list of values. Lastly, we define key argument with a lambda function, where we specify the key to be the second item of tuple, that is, item[1].<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i, j in sorted(data.items(),reverse=True,key=lambda item:item[1]):<br>        print i, j<br><br>c 3<br>b 2<br>a 1<\/pre>\n\n\n\n<p>This approach is useful if the original order of key-value pairs is not as per your requirement and you want to change it as per your requirement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Iterate_over_Multiple_dictionaries\"><\/span>6. Iterate over Multiple dictionaries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes, software developers need to loop over multiple dictionaries at the same time. There are several ways to do this in Python. In this article, we will learn how to do this one by one.<\/p>\n\n\n\n<p>Let us say you have two dictionaries.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {'a':1,'b':2,'c':3}<br>data2 = {'d':4,'e':5,'f':6}<\/pre>\n\n\n\n<p>You can easily merge these two dictionaries and iterate through the merged result using unpacking operator(**). In case these dictionaries have duplicate keys, the value of the rightmost dictionary will overwrite other values. Here is an example to easily iterate over multiple dictionaries data and data2. We need to mention dictionaries in curly braces {} in a comma-separated manner. Each of these dictionaries should be prefixed by unpacking operator. Then the entire list of dictionaries within curly braces can be treated as a single dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i,j in {**data, **data2}.items():<br>     print(i, \"-&gt;\", j)<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a -&gt; 1<br>b -&gt; 2<br>c -&gt; 3<br>d -&gt; 4<br>e -&gt; 5<br>f -&gt; 6<\/pre>\n\n\n\n<p>Please note, in this case, the original dictionaries remain unchanged.<\/p>\n\n\n\n<p>Python also provides a couple of other functions for this purpose. First one is a special container called ChainMap. It allows you to create a single dictionary like object that spans across multiple dictionaries. It can be accessed and used as a single dictionary. Here is an example to use a ChainMap constructor to create a unified view of data and data2 dictionaries.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from collections import ChainMap<br>data3 = ChainMap(data, data2)<br><br>for i, j in data3.items():<br>    print(i,'-&gt;',j)<\/pre>\n\n\n\n<p>In the above code, we first import ChainMap object from collections. We use it to create a unified view of dictionaries data and data2. We loop through the items of this dictionary to display key and values. Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">d -&gt; 4<br>e -&gt; 5<br>f -&gt; 6<br>a -&gt; 1<br>b -&gt; 2<br>c -&gt; 3<\/pre>\n\n\n\n<p>Again, please note, this does not alter the original dictionaries in any way.<\/p>\n\n\n\n<p>You can also use chain() function from itertools library for this purpose. It accepts multiple iterables such as dictionaries, as arguments, and returns an iterator that yields all elements from all of the individual iterables. Every time chain() function is accessed, it returns the next element of the iterable, until all elements are exhausted.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from itertools import chain<br>data3 = chain(data.items(), data2.items())<br><br>for i, j in data3:<br>    print(i,'-&gt;',j)<\/pre>\n\n\n\n<p>In the above code, we pass data.items() and data2.items() to chain() function to get an iterator that can loop through all elements of both dictionaries. In each iteration of for loop, we print the key and values together.<\/p>\n\n\n\n<p>Here also, the original dictionaries remain unchanged.<\/p>\n\n\n\n<p>This is a very useful feature in Python. Otherwise, it would have become very tedious to loop through multiple dictionaries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Using_map_function\"><\/span>7. Using map() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes, we may need to not only loop through all elements of a dictionary but also apply a specific function to each element. For this purpose, we can use map() function. It takes a function object along with an iterable and applies the function to each element of the iterable. Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {'a':1,'b':2,'c':3}<br><br>def double(item):<br>    return item[0],2*item[1]<br>    <br>print(dict(map(double, data.items())))<\/pre>\n\n\n\n<p>In the above code, we first define a dictionary data. We then define a simple python function that takes in a key-value pair, and returns the key along with the 2 x value. We call map() function with two arguments &#8211; the double function and the items of data dictionary. We pass the result of map() function, that is, a map object to dict() constructor to convert it into a dictionary. Here is the output. You will see that each value of dictionary has been doubled. This is because the double() function looped through the dictionary and doubled each key-value pair&#8217;s value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'a': 2, 'b': 4, 'c': 6}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8_Using_filter_function\"><\/span>8. Using filter() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes, you may need to loop through a dictionary and filter certain items from it, using one or more conditions. For this purpose, you can use filter() function. It also takes an iterator and a function object as arguments, and returns an iterator to those elements from original dictionary that satisfy the filter condition.<\/p>\n\n\n\n<p>As in the case of map() function, we will first define a simple Python function that checks if an item is odd or not using modulus (%) operator, and use it with filter() function as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {'a':1,'b':2,'c':3}<br><br>def check(item):<br>    return item[1]%2==1<br><br>print(dict(filter(check, data.items())))<\/pre>\n\n\n\n<p>In the above code, we pass function object of check() along with the items of data dictionary to filter() function. The filter function will iterate over each key-value pair of data dictionary and check whether the value of each item is odd or not. Those items that pass the condition are returned. So you get a dictionary where each item value is an odd number. Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'a': 1, 'c': 3}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt several important ways to iterate over dictionaries using for loops. We learnt how to loop over keys only, values only and a combination of keys and values, in a dictionary. We have also learnt different ways to iterate over multiple dictionaries at the same time. Then we learnt how to iterate over a dictionary and execute specific function for each item, using map() function. Lastly, we learnt how to iterate over dictionary and filter its items for one or more conditions using filter() function. You can use any of these solutions as per your requirement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span>FAQ<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. How to iterate over keys in Python dictionary?<\/strong><\/p>\n\n\n\n<p>You can use keys() function available for each dictionary to get a list of dictionary keys. You can easily loop over this list. Alternatively, you can directly iterate over the dictionary. It will return the key value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {'a':1, 'b':2, 'c':3}<br><br>for i in data:<br>    print i<\/pre>\n\n\n\n<p><strong>2. How to iterate over values in Python dictionary?<\/strong><\/p>\n\n\n\n<p>Use the above method to get keys of your dictionary. While looping through the keys, use the key to get value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {'a':1, 'b':2, 'c':3}<br><br>for i in data:<br>    print data[i]<\/pre>\n\n\n\n<p><strong>3. How to iterate over both keys and values in dictionary?<\/strong><\/p>\n\n\n\n<p>As shown above, in each iteration, you will have access to both key as well as value. You can use them to iterate over both key and values in dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {'a':1, 'b':2, 'c':3}<br><br>for i in data:<br>    print(i, data[i])<\/pre>\n\n\n\n<p><strong>4. How to iterate through dictionary in reverse order?<\/strong><\/p>\n\n\n\n<p>You can call sorted() function, with your dictionary as one of the argument, and reverse=True as the other argument. This will reverse sort the dictionary and allow you to iterate the dictionary in reverse order.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data = {'a':1, 'b':2, 'c':3}<br>&gt;&gt;&gt; for i in sorted(data, reverse=True):<br>       print i, data[i]<br><br>c 3<br>b 2<br>a 1<\/pre>\n\n\n\n<p><strong>5. How to iterate through multiple dictionaries?<\/strong><\/p>\n\n\n\n<p>You can iterate through multiple dictionaries using unpacking operator, map() function or filter() function.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/\">How to Find Given Item in Python List<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/\">How to Flatten List of Lists in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\">How to Access Index Value in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python developers need to iterate over dictionaries using for loops. Here are different ways to do this.<\/p>\n","protected":false},"author":1,"featured_media":6568,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[372],"class_list":["post-6511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-iterate-over-dict"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Iterate Over Dictionaries Using For Loops - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Python developers need to iterate over dictionaries using for loops. Here are different ways to do this.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Iterate Over Dictionaries Using For Loops - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Python developers need to iterate over dictionaries using for loops. Here are different ways to do this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-09T07:36:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T04:33:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"245\" \/>\n\t<meta property=\"og:image:height\" content=\"164\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Iterate Over Dictionaries Using For Loops\",\"datePublished\":\"2025-01-09T07:36:39+00:00\",\"dateModified\":\"2025-05-20T04:33:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\"},\"wordCount\":2297,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1\",\"keywords\":[\"iterate over dict\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\",\"name\":\"How to Iterate Over Dictionaries Using For Loops - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1\",\"datePublished\":\"2025-01-09T07:36:39+00:00\",\"dateModified\":\"2025-05-20T04:33:20+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Python developers need to iterate over dictionaries using for loops. Here are different ways to do this.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1\",\"width\":245,\"height\":164,\"caption\":\"iterate over dictionary using for loop\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Iterate Over Dictionaries Using For Loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/\"],\"url\":\"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Iterate Over Dictionaries Using For Loops - Ubiq BI","description":"Python developers need to iterate over dictionaries using for loops. Here are different ways to do this.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/","og_locale":"en_US","og_type":"article","og_title":"How to Iterate Over Dictionaries Using For Loops - Ubiq BI","og_description":"Python developers need to iterate over dictionaries using for loops. Here are different ways to do this.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-01-09T07:36:39+00:00","article_modified_time":"2025-05-20T04:33:20+00:00","og_image":[{"width":245,"height":164,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Iterate Over Dictionaries Using For Loops","datePublished":"2025-01-09T07:36:39+00:00","dateModified":"2025-05-20T04:33:20+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/"},"wordCount":2297,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1","keywords":["iterate over dict"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/","name":"How to Iterate Over Dictionaries Using For Loops - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1","datePublished":"2025-01-09T07:36:39+00:00","dateModified":"2025-05-20T04:33:20+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Python developers need to iterate over dictionaries using for loops. Here are different ways to do this.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1","width":245,"height":164,"caption":"iterate over dictionary using for loop"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Iterate Over Dictionaries Using For Loops"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/iterate-over-dictionary-using-for-loop.jpg?fit=245%2C164&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1H1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=6511"}],"version-history":[{"count":70,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6511\/revisions"}],"predecessor-version":[{"id":8303,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6511\/revisions\/8303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/6568"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=6511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=6511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=6511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}