{"id":10320,"date":"2025-08-24T00:55:46","date_gmt":"2025-08-24T07:55:46","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=10320"},"modified":"2025-08-24T00:55:46","modified_gmt":"2025-08-24T07:55:46","slug":"462-sorting-a-dictionary-by-key-or-value-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2025\/08\/462-sorting-a-dictionary-by-key-or-value-in-python\/","title":{"rendered":"#462 Sorting a Dictionary by Key or Value in Python"},"content":{"rendered":"<p>In Python, dictionaries are unordered collections of key-value pairs. Sometimes, we need to sort a dictionary either by its keys or by its values. This can be done easily using the built-in sorted() function.<\/p>\n<pre class=\"lang:python decode:true \"># Example dictionary\nd = {'a': 3, 'c': 1, 'b': 2}\n\n# Sort dictionary items by value\nsorted_by_val = sorted(d.items(), key=lambda x: x[1])\nprint(sorted_by_val)  # [('c', 1), ('b', 2), ('a', 3)]\n\n# Sort dictionary items by key\nsorted_by_key = sorted(d.items(), key=lambda x: x[0])\nprint(sorted_by_key)  # [('a', 3), ('b', 2), ('c', 1)]<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:python decode:true \">[('c', 1), ('b', 2), ('a', 3)]\n[('a', 3), ('b', 2), ('c', 1)]<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, dictionaries are unordered collections of key-value pairs. Sometimes, we need to sort a dictionary either by its keys or by its values. This can be done easily using the built-in sorted() function. # Example dictionary d = {&#8216;a&#8217;: 3, &#8216;c&#8217;: 1, &#8216;b&#8217;: 2} # Sort dictionary items by value sorted_by_val = sorted(d.items(), key=lambda [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[3],"class_list":["post-10320","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-2Gs","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1682,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-5\/","url_meta":{"origin":10320,"position":0},"title":"#318 Use lambda as sorting key in python","author":"gantovnik","date":"2022-11-22","format":false,"excerpt":"One of the most common situations where a lambda comes in handy is when specifying a key function, which is a callable that returns the part of a collection or object that should be used for sorting. Output:","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1718,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2\/","url_meta":{"origin":10320,"position":1},"title":"#325 Finding the intersection points between two functions using python","author":"gantovnik","date":"2022-12-13","format":false,"excerpt":"","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":10657,"url":"https:\/\/gantovnik.com\/bio-tips\/2025\/11\/itertools-dropwhile-python-example\/","url_meta":{"origin":10320,"position":2},"title":"#465 Using itertools.dropwhile in Python: Skip Values Until a Condition Fails","author":"gantovnik","date":"2025-11-04","format":false,"excerpt":"Python\u2019s itertools module provides a robust set of tools for working with iterators. One of the lesser-known but convenient functions is itertools.dropwhile(). It allows you to skip elements in an iterable as long as a specific condition remains true \u2014 and once the condition fails, it yields the rest of\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1786,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/340-lorenz-attractors-using-python\/","url_meta":{"origin":10320,"position":3},"title":"#340 Lorenz attractors using python","author":"gantovnik","date":"2023-01-17","format":false,"excerpt":"","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":625,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/97-finding-roots-on-an-interval-with-the-bisection-method\/","url_meta":{"origin":10320,"position":4},"title":"#97 Finding roots on an interval with the bisection method.","author":"gantovnik","date":"2020-09-16","format":false,"excerpt":"#97 Finding roots on an interval with the bisection method. One of the simplest algorithms for solving equations of the form f(x)=0 is called the bisection method. Output:","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1738,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2\/","url_meta":{"origin":10320,"position":5},"title":"#329 Golden section method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py ex329.py","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex329.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/10320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=10320"}],"version-history":[{"count":1,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/10320\/revisions"}],"predecessor-version":[{"id":10321,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/10320\/revisions\/10321"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=10320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=10320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=10320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}