{"id":13213,"date":"2025-08-19T06:02:27","date_gmt":"2025-08-19T06:02:27","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13213"},"modified":"2025-08-19T06:52:02","modified_gmt":"2025-08-19T06:52:02","slug":"sort-dictionary-by-value","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/sort-dictionary-by-value","title":{"rendered":"How to Sort a Dictionary By Value?"},"content":{"rendered":"<h3>Use the sorted() function with a dict.items()<\/h3>\n<p>Here\u2019s how to do it <strong>without converting to a list of dictionaries:<\/strong><\/p>\n<p><strong>Sort by values (ascending):<\/strong><\/p>\n<pre class=\"lang:adddarkplain\">sorted_items = sorted(my_dict.items(), key=lambda item: item[1])<\/pre>\n<p><strong>Sort by values (descending):<\/strong><\/p>\n<pre class=\"lang:adddarkplain\">sorted_items = sorted(my_dict.items(), key=lambda item: item[1],reverse=True) <\/pre>\n<p>This returns a list of (key, value) tuples sorted by value.<\/p>\n<h3>Example:<\/h3>\n<pre class=\"lang:adddarkplain\">my_dict = {\r\n   'apple': 10,\r\n   'banana': 2,\r\n   'cherry': 7\r\n}\r\n\r\n# Ascending sort by value\r\nsorted_items = sorted(my_dict.items(), key=lambda x: x[1])\r\nprint(sorted_items)\r\n# Output: [('banana', 2), ('cherry', 7), ('apple', 10)]\r\n\r\n# Descending sort by value\r\nsorted_items = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)\r\nprint(sorted_items)\r\n# Output: [('apple', 10), ('cherry', 7), ('banana', 2)]<\/pre>\n<h3>Optional: Convert back to a dictionary<\/h3>\n<p>If you want the result back as a dict (e.g., for further use):<\/p>\n<pre class=\"lang:adddarkplain\">sorted_dict = dict(sorted(my_dict.items(), key=lambda x: x[1]))<\/pre>\n<p><strong>Note:<\/strong> Regular Python dicts preserve insertion order as of Python 3.7+ (officially guaranteed in 3.8+), so this will maintain the sorted order.<\/p>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/what-is-python-used-for\" target=\"_blank\">Why Use Python<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use the sorted() function with a dict.items() Here\u2019s how to do it without converting to a list of dictionaries: Sort by values (ascending): sorted_items = sorted(my_dict.items(), key=lambda item: item[1]) Sort by values (descending): sorted_items = sorted(my_dict.items(), key=lambda item: item[1],reverse=True) This returns a list of (key, value) tuples sorted by value. Example: my_dict = { &#8216;apple&#8217;: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13219,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-13213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13213"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13213"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13213\/revisions"}],"predecessor-version":[{"id":13214,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13213\/revisions\/13214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13219"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}