{"id":80199,"date":"2021-02-12T09:00:14","date_gmt":"2021-02-12T03:30:14","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80199"},"modified":"2021-02-12T09:00:14","modified_gmt":"2021-02-12T03:30:14","slug":"ordereddict-module-in-python","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/","title":{"rendered":"OrderedDict Module in Python with Examples"},"content":{"rendered":"<p>In this article, we will read about the Python Ordereddict module, and will see the difference in its execution with different attributes through codes.<\/p>\n<p>Covering its various functions and getting well along with its attributes will be a major focus of this article, so let\u2019s get started.<\/p>\n<h2>What is OrderedDict in Python?<\/h2>\n<p>Ordereddict is a method of insertion of the loop variables in an empty list in python.<\/p>\n<p>Ordereddict module is predefined in the python library, and hence needs to be imported in the code before its usage.<\/p>\n<p>The mutable lists work as an ordered dict in a code for any arbitrary constant value in the code. The coder as well as the user can define such lists in runtime of the code.<\/p>\n<p><strong>OrderedDict Example in Python:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">thisistech = {} \nthisistech['woah'] = 1\nthisistech['why'] = 2\nthisistech['hey'] = 3\nthisistech['hello'] = 4\n  \nfor key, value in thisistech.items(): \n    print(key, value) \n  \nprint(\"ordered dict is this\") \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">woah 1<br \/>\nwhy 2<br \/>\nhey 3<br \/>\nhello 4<br \/>\nordered dict is this<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>Python OrderedDict Syntax<\/h3>\n<p>OrderedDict in python is a collection of multiple lists, but the coder imports it from the module \u2018collection\u2019 before any execution.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; from collections import OrderedDict\n<\/pre>\n<p><strong>Key value Change:<\/strong> This signifies to the modulation in the change of the main key in the list. It can be simply done by inserting a new element in the place of the old one by import function.<\/p>\n<p><strong>Python OrderedDict Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from collections import OrderedDict \nprint(\"Before the execution\") \nthisistech= OrderedDict() \nthisistech['woah'] = 0\nthisistech['why'] = 2\nthisistech['here'] = 3\nthisistech['how'] = 4\nfor key, value in thisistech.items(): \n    print(key, value) \n  \nprint(\"after execution\") \nthisistech['yayay'] = 90\nfor key, value in thisistech.items(): \n    print(key, value) \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Before the execution<br \/>\nwoah 0<br \/>\nwhy 2<br \/>\nhere 3<br \/>\nhow 4<br \/>\nafter execution<br \/>\nwoah 0<br \/>\nwhy 2<br \/>\nhere 3<br \/>\nhow 4<br \/>\nyayay 90<br \/>\n&gt;&gt;&gt;<\/div>\n<p><strong>Deletion and Re-Inserting:<\/strong> It is common that at places the coder needs to delete the previous value and insert the new one. Or probably the already defined value if wrongly inserted may need to get deleted. This can be simply done by the delete function in the list.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from collections import OrderedDict \n  \nprint(\"Before execution:\\n\") \nthisistech = OrderedDict() \nthisistech['woah'] = 1\nthisistech['where'] = 2\nthisistech['when'] = 3\nthisistech['why'] = 4\n  \nfor key, value in thisistech.items(): \n    print(key, value) \n  \nprint(\"\\nAfter deletion:\\n\") \nthisistech.pop('when') \nfor key, value in thisistech.items(): \n    print(key, value) \n  \nprint(\"\\nAfter re-inserting execution:\\n\") \nthisistech['when'] = 3\nfor key, value in thisistech.items(): \n    print(key, value)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p>Before execution:<\/p>\n<p>woah 1<br \/>\nwhere 2<br \/>\nwhen 3<br \/>\nwhy 4<\/p>\n<p>After deletion:<\/p>\n<p>woah 1<br \/>\nwhere 2<br \/>\nwhy 4<\/p>\n<p>After re-inserting execution:<\/p>\n<p>woah 1<br \/>\nwhere 2<br \/>\nwhy 4<br \/>\nwhen 3<br \/>\n&gt;&gt;&gt;<\/p>\n<\/div>\n<h3>OrderedDict Reverse Iteration<\/h3>\n<p>The values in a code can be displayed in reverse order on the final output. And this is best done by reverse iteration in the code, for example:<\/p>\n<p><strong>OrderedDict Reverse Iteration Example in Python:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">thisistech=['w','l','k']\nfor r in reversed(thisistech):\n    print(r)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">k<br \/>\nl<br \/>\nw<br \/>\n&gt;&gt;&gt;<\/div>\n<h4>Changing the Value of a Specified Key<\/h4>\n<p>When the value of a specified key is changed by the user, the numbering of keys does not change. This is simply done by manipulating one list\u2019s elements in the other list.<\/p>\n<h4>Equality<\/h4>\n<p>This basically compares the two lists element wise, before compiling them in a final list. The two matched elements are always taken as one only in the final list.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def thisistech():\n    \n   thisistech['p']=1\n   thisistech['q']=2\nprint(thisistech)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&lt;function d at 0x037AFCD0&gt;<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>Python Popitem ()<\/h3>\n<p>The popItem() method in Python may take 0 or 1 argument that means on and off in binary language. If a passed argument is a null set then, the last element is assumed to be true by default.<\/p>\n<p><strong>Python OrderedDict Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">thisistech=OrderedDict()\nthisistech['d']=4\nthisistech['e']=5\nprint(d)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">error<\/div>\n<h2>Conclusion<\/h2>\n<p>In this article we\u2019ve read about the functions, attributes and methods of defaultdict module in python. Default dict module is mostly used in program oriented codes, or the reports and generally in technical analysis also. But it needs good practice for applicable usage also. Happy pythonning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will read about the Python Ordereddict module, and will see the difference in its execution with different attributes through codes. Covering its various functions and getting well along with its&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80222,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3360,3361,3362],"class_list":["post-80199","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-ordereddict-in-python","tag-ordereddict-module-in-python","tag-python-ordereddict"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>OrderedDict Module in Python with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"OrderedDict preserves the order in which the keys are inserted. Learn about OrderedDict Module in Python with syntax and examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OrderedDict Module in Python with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"OrderedDict preserves the order in which the keys are inserted. Learn about OrderedDict Module in Python with syntax and examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-12T03:30:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-orderedDict-module.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OrderedDict Module in Python with Examples - TechVidvan","description":"OrderedDict preserves the order in which the keys are inserted. Learn about OrderedDict Module in Python with syntax and examples.","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:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/","og_locale":"en_US","og_type":"article","og_title":"OrderedDict Module in Python with Examples - TechVidvan","og_description":"OrderedDict preserves the order in which the keys are inserted. Learn about OrderedDict Module in Python with syntax and examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-02-12T03:30:14+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-orderedDict-module.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"OrderedDict Module in Python with Examples","datePublished":"2021-02-12T03:30:14+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/"},"wordCount":517,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-orderedDict-module.jpg","keywords":["OrderedDict in Python","OrderedDict Module in Python","python ordereddict"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/","url":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/","name":"OrderedDict Module in Python with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-orderedDict-module.jpg","datePublished":"2021-02-12T03:30:14+00:00","description":"OrderedDict preserves the order in which the keys are inserted. Learn about OrderedDict Module in Python with syntax and examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-orderedDict-module.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-orderedDict-module.jpg","width":1200,"height":628,"caption":"Python orderedDict module"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/ordereddict-module-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"OrderedDict Module in Python with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80199"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80199\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80222"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}