{"id":12971,"date":"2025-07-31T07:26:55","date_gmt":"2025-07-31T07:26:55","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12971"},"modified":"2025-07-31T07:51:30","modified_gmt":"2025-07-31T07:51:30","slug":"sort-a-dictionary-by-value","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/sort-a-dictionary-by-value","title":{"rendered":"Sort a Dictionary By Value"},"content":{"rendered":"<h2>Use the sorted() function with a dict.items()<\/h2>\n<p>Here\u2019s how to do it <strong>without converting to a list of dictionaries:<\/strong><\/p>\n<h3>Sort by values (ascending):<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nsorted_items = sorted(my_dict.items(), key=lambda item: item[1])\r\n<\/pre>\n<h3>Sort by values (descending):<br \/>\n<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nsorted_items = sorted(my_dict.items(), key=lambda item: item[1], reverse=True)\r\n<\/pre>\n<p>This returns a list of (key, value) tuples sorted by value.<\/p>\n<h3>Example:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nmy_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)]\r\n<\/pre>\n<h2>Optional: Convert back to a dictionary<\/h2>\n<p>If you want the result back as a dict (e.g., for further use):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nsorted_dict = dict(sorted(my_dict.items(), key=lambda x: x[1]))\r\n<\/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 = { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12972,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-12971","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\/12971"}],"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=12971"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12971\/revisions"}],"predecessor-version":[{"id":12974,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12971\/revisions\/12974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12972"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}