{"id":847,"date":"2021-06-14T13:44:05","date_gmt":"2021-06-14T08:14:05","guid":{"rendered":"https:\/\/techieshouts.com\/?p=847"},"modified":"2022-08-09T19:04:14","modified_gmt":"2022-08-09T13:34:14","slug":"how-to-merge-and-sort-dictionaries","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/how-to-merge-and-sort-dictionaries\/","title":{"rendered":"How to Merge and Sort dictionaries"},"content":{"rendered":"\n<p> In Python, we can merge two dictionary objects very easily.  First, we create two dictionaries then we can merge and sort the dictionaries based on the values. In python 2.x we can use the following code,<\/p>\n\n\n\n<h3>Python 2.x<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(\"Example for merging dictionaries\")\nprint(\"Creation of dic1\")\ndic1={'key1':1, 'key2':2}\nprint(dic1)\nprint(\"Creation of dic2\")\ndic2={'key3':3, 'key4':4}\nprint(dic2)\ndic3=dict(dic1, **dic2)\nprint(\"Before Sorting\")\nprint(dic3)\nsorted(dic3.items(), key=lambda x: x[1])\nprint(\"After Sorting\")\nprint(dic3)\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Output:\nExample for merging dictionaries\nCreation of dic1\n{'key2':2, 'key1':1}\nCreation of dic2\n{'key3':3, 'key4':4}\nBefore Sorting\n{'key3': 3, 'key2': 2, 'key1': 1, 'key4': 4}\nAfter Sorting\n[('key1', 1), ('key2', 2), ('key3', 3), ('key4', 4)]<\/pre>\n\n\n\n<p>In the below example, the two dictionaries are created and then merge the dictionary keys with the values. After that, we can sort the keys based on the values.<\/p>\n\n\n\n<h3>Python 3.x<\/h3>\n\n\n\n<h4>Method: 1<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(\"Example for merging dictionaries\")\nprint(\"creation of dic1\")\ndic1={'key1':4, 'key2':7}\nprint(dic1)\nprint(\"Creation of dic2\")\nprint(dic2)\ndic2={'key3':8, 'key4':6}\ndic3={**dic1, **dic2}\nprint(\"Before Sorting\")\nprint(dic3)\nsorted(dic3.items(), key=lambda x: x[1])\nprint(\"After sorting\")\nprint(dic3)<\/pre>\n\n\n\n<h4>Method: 2<\/h4>\n\n\n\n<p>By using the operator function, we can use the below code. But, the output will be the same for both the codes.  <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import operator \nprint(\"Example for merging dictionaries\")\nprint(\"creation of dic1\")\ndic1={'key1':4, 'key2':7}\nprint(dic1)\nprint(\"Creation of dic2\")\nprint(dic2)\ndic2={'key3':8, 'key4':6}\ndic3={**dic1, **dic2}\nprint(\"Before Sorting\")\nprint(dic3)\nsorted(dic3.items(), key=operator.itemgetter(1))\nprint(\"After sorting\")\nprint(dic3)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Output:\nExample for merging dictionaries\nCreation of dic1\n{'key1':4, 'key2':7}\nCreation of dic2\n{'key3':8, 'key4':6}\nBefore Sorting\n{'key1': 4, 'key2': 7, 'key3': 8, 'key4': 6}\nAfter Sorting\n[('key1', 4), ('key4', 6), ('key2', 7), ('key3', 8)]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Python, we can merge two dictionary objects very easily. First, we create two dictionaries then we can merge and sort the dictionaries based on the values. In python 2.x we can use the following code, Python 2.x In the below example, the two dictionaries are created and then merge the dictionary keys with the\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/how-to-merge-and-sort-dictionaries\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,14],"tags":[112,113,114],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/847"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=847"}],"version-history":[{"count":19,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/847\/revisions"}],"predecessor-version":[{"id":884,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/847\/revisions\/884"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}