{"id":2267,"date":"2020-01-06T07:34:33","date_gmt":"2020-01-06T07:34:33","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2267"},"modified":"2022-08-06T13:17:21","modified_gmt":"2022-08-06T13:17:21","slug":"python-ordereddict","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-ordereddict","title":{"rendered":"Python OrderedDict"},"content":{"rendered":"\n<p>OrderedDict is a subclass of the dictionary, which maintains the order in which the elements\/items are added to it.<\/p>\n\n\n\n<p> OrderedDict\u00a0preserves the order of insertion of the elements. A default <strong>dict does not save the order<\/strong> and results in the iteration in an arbitrary order.<\/p>\n\n\n\n<p>Initially, we need to import the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-collections\" class=\"rank-math-link\">collections module<\/a> to use the OrderedDict library module. We can also import only the OrderedDict class from the collections module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport collections\n\nfrom collections import OrderedDict\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python OrderedDict functionalities<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Creation of OrderedDict object<\/li><li>Adding items to OrderedDict<\/li><li>Replacing items from OrderedDict<\/li><li>Removal of items from OrderedDict<\/li><li>Key-Value Change<\/li><li>move_to_end() function<\/li><li>OrderedDict pop item<\/li><li>Reverse iteration<\/li><li>OrderedDict Equality Test<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Creation of OrderedDict object<\/h3>\n\n\n\n<p>The <code><strong>OrderedDict()<\/strong><\/code> function is used for the creation of the object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nordered_input = OrderedDict(my_input)\nprint(ordered_input)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Adding items to OrderedDict<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nordered_input = OrderedDict(my_input)\n#print(ordered_input)\n\nprint(&quot;Adding item to OrderedDict....&quot;)\nordered_input&#x5B;&#039;Hyderabad&#039;] = &#039;Karnataka&#039;\nprint(ordered_input)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Adding item to OrderedDict....\nOrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar'), ('Hyderabad', 'Karnataka')])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Replacing items from OrderedDict<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nordered_input = OrderedDict(my_input)\n#print(ordered_input)\n\nprint(&quot;Replacing item from OrderedDict....&quot;)\nordered_input&#x5B;&#039;Pune&#039;] = &#039;Satara&#039;\nprint(ordered_input)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Adding items to OrderedDict....\nOrderedDict([('Pune', 'Satara'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Removal of items from OrderedDict<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nordered_input = OrderedDict(my_input)\n#print(ordered_input)\n\nprint(&#039;Removal of item from OrderedDict....&#039;)\nordered_input.pop(&#039;Pune&#039;)\nprint(ordered_input)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Removal of item from OrderedDict....\nOrderedDict([('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Key-Value change in an OrderedDict<\/h3>\n\n\n\n<p>In an OrderedDict, if the value corresponding to a particular key is altered, the position\/index of that key remains unchanged.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nprint(&#039;Before the change.....&#039;)\nordered_input = OrderedDict(my_input)\nprint(ordered_input)\n\nprint(&#039;After the change.....&#039;)\nordered_input&#x5B;&#039;Pune&#039;] = &#039;Kiara&#039;\nprint(ordered_input)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before the change.....\nOrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])\nAfter the change.....\nOrderedDict([('Pune', 'Kiara'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. move_to_end() function<\/h3>\n\n\n\n<p>The <code><strong>move_to_end()<\/strong><\/code> function moves a particular key-value pair to the end of the dict.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nprint(&#039;Before using the move_to_end().....&#039;)\nordered_input = OrderedDict(my_input)\nprint(ordered_input)\n\nprint(&#039;After using the move_to_end().....&#039;)\nordered_input.move_to_end(&#039;Pune&#039;)\nprint(ordered_input)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before using the move_to_end().....\nOrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])\nAfter using the move_to_end().....\nOrderedDict([('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar'), ('Pune', 'Maharashtra')])\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. OrderedDict popitem<\/h3>\n\n\n\n<p>This function pops out and returns the last element as output.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nprint(&#039;Original input dict.....&#039;)\nordered_input = OrderedDict(my_input)\nprint(ordered_input)\n\n\nresult = ordered_input.popitem(True)\nprint(&#039;The popped item is: &#039;)\nprint(result)\n\nprint(ordered_input)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original input dict.....\nOrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])\nThe popped item is: \n('Orrisa', 'Bhubhaneshwar')\nOrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat')])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. Reverse iteration<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n\nmy_input = {&#039;Pune&#039;: &#039;Maharashtra&#039;, &#039;Ahemadnagar&#039;: &#039;Gujarat&#039;, &#039;Orrisa&#039;: &#039;Bhubhaneshwar&#039;}\n\n# creating ordered dict from dict\nprint(&#039;Original input dict.....&#039;)\nordered_input = OrderedDict(my_input)\nprint(ordered_input)\n\nprint(&#039;Reversed OrderedDict.....&#039;)\n\nfor elements in reversed(ordered_input):\n    print(elements)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original input dict.....\nOrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])\nReversed OrderedDict.....\nOrrisa\nAhemadnagar\nPune\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. OrderedDict Equality Test<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import OrderedDict\n\n# creating regular dict..\nmy_input1 = {1:&#039;one&#039; , 2:&#039;two&#039;}\nmy_input2 = {2:&#039;two&#039; , 1:&#039;one&#039;}\n\n#creating ordereddict..\nordered_input1 = OrderedDict({1:&#039;one&#039; , 2:&#039;two&#039;})\nordered_input2 = OrderedDict({2:&#039;two&#039; , 1:&#039;one&#039;})\n\nprint(my_input1 == ordered_input1)\nprint(my_input1 == my_input2)\nprint(ordered_input1 == ordered_input2)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nTrue\nFalse<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the difference between regular Dictionary and OrderedDict and have had a look at the functionalities offered by OrderedDict.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python OrderedDict<\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/collections.html#collections.OrderedDict\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">OrderedDict documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>OrderedDict is a subclass of the dictionary, which maintains the order in which the elements\/items are added to it. OrderedDict\u00a0preserves the order of insertion of the elements. A default dict does not save the order and results in the iteration in an arbitrary order. Initially, we need to import the collections module to use the [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,2],"tags":[],"class_list":["post-2267","post","type-post","status-publish","format-standard","hentry","category-dictionary","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2267"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2267\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}