{"id":12423,"date":"2025-02-17T09:44:09","date_gmt":"2025-02-17T09:44:09","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=12423"},"modified":"2025-02-18T11:31:00","modified_gmt":"2025-02-18T11:31:00","slug":"sort-dictionary-by-value-in-python","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/sort-dictionary-by-value-in-python\/","title":{"rendered":"How to Sort Dictionary by Value in Python?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/sort-dictionary-by-value-in-python\/#What_is_a_Dictionary_in_Python\" title=\"What is a Dictionary in Python?\">What is a Dictionary in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/sort-dictionary-by-value-in-python\/#How_to_Sort_a_Dictionary_by_Value_Step-by-Step\" title=\"How to Sort a Dictionary by Value (Step-by-Step)\">How to Sort a Dictionary by Value (Step-by-Step)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/sort-dictionary-by-value-in-python\/#Use_Cases_for_Sorting_Dictionaries_by_Value\" title=\"Use Cases for Sorting Dictionaries by Value\">Use Cases for Sorting Dictionaries by Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/sort-dictionary-by-value-in-python\/#Key_Takeaways\" title=\"Key Takeaways\">Key Takeaways<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>A dictionary is a very commonly used data structure in Python due to its flexibility for storing and managing data as key-value pairs. Now, what happens when you need to organize (or sort) the data stored in a dictionary based on its <em>values<\/em>\u2014like ranking users by scores, filtering top-selling products, or analyzing word frequencies? Dictionaries in Python do not maintain order based on values, making sorting necessary when working with ranked data.<\/p>\n\n\n\n<p>In this article, we will learn how to sort dictionaries by value using Python\u2019s built-in tools. Whether you\u2019re preparing data for visualization, building a leaderboard, or optimizing workflows, mastering this skill will streamline your coding projects. We\u2019ll break down the process step-by-step, provide clear examples, and explore real-world scenarios where sorting dictionaries unlock powerful insights. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Dictionary_in_Python\"><\/span>What is a Dictionary in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A <strong>dictionary<\/strong> in Python is a built-in data structure that stores data as <strong>key-value pairs<\/strong>. Think of it like a real-life dictionary: you look up a word (the <em>key<\/em>) to find its definition (the <em>value<\/em>). <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/logicmojo.com\/python-dictionary\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1075\" height=\"397\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/02\/pythondict.png\" alt=\"Dictionary in Python\" class=\"wp-image-12455\" style=\"width:458px;height:auto\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/02\/pythondict.png 1075w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/02\/pythondict-300x111.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/02\/pythondict-1024x378.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/02\/pythondict-768x284.png 768w\" sizes=\"auto, (max-width: 1075px) 100vw, 1075px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Dictionaries are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Unordered<\/strong> (in Python 3.6 and earlier; insertion order is preserved in Python 3.7+).<\/li>\n\n\n\n<li><strong>Mutable<\/strong> (can be changed after creation).<\/li>\n\n\n\n<li>Accessed via <strong>keys<\/strong>, not numerical indexes.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s look at an example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"student_grades = {\n    &quot;Alice&quot;: 92,\n    &quot;Bob&quot;: 85,\n    &quot;Charlie&quot;: 78,\n    &quot;Diana&quot;: 95\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">student_grades = {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&quot;Alice&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">92<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&quot;Bob&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">85<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&quot;Charlie&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">78<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&quot;Diana&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">95<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Here, the keys are student names (strings), and values are their grades (integers).<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>To learn more about the dictionary data structure, check out this comprehensive <a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#dictionaries\" target=\"_blank\" rel=\"noopener\">guide<\/a> in Python&#8217;s official documentation.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Sort_a_Dictionary_by_Value_Step-by-Step\"><\/span>How to Sort a Dictionary by Value (Step-by-Step)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before I show you how to do the sorting, let&#8217;s quickly talk about what <em>sort by value <\/em>even means. <\/p>\n\n\n\n<p>Sorting a dictionary by value means rearranging its key-value pairs based on the <em>values<\/em> instead of the keys. For example, sorting <code>student_grades<\/code> by grades (values) would arrange the students from lowest to highest score (or vice versa).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a Sample Dictionary<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"fruits = {&quot;apple&quot;: 5, &quot;banana&quot;: 2, &quot;cherry&quot;: 8, &quot;date&quot;: 3}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">fruits = {<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&quot;banana&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&quot;cherry&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&quot;date&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Use <code>sorted()<\/code> to Sort by Value<\/h3>\n\n\n\n<p>The <code>sorted()<\/code> function returns a <strong>sorted list<\/strong> of the dictionary\u2019s key-value pairs (as tuples). Use the <code>key<\/code> parameter to specify sorting by the second element of each tuple (the value).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Ascending Order (Low to High):<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sorted_items = sorted(fruits.items(), key=lambda x: x[1])\n# sorted_items is a list: [('banana', 2), ('date', 3), ('apple', 5), ('cherry', 8)]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">sorted_items = <\/span><span style=\"color: #DCDCAA\">sorted<\/span><span style=\"color: #D4D4D4\">(fruits.items(), <\/span><span style=\"color: #9CDCFE\">key<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">lambda<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">: x[<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># sorted_items is a list: [(&#39;banana&#39;, 2), (&#39;date&#39;, 3), (&#39;apple&#39;, 5), (&#39;cherry&#39;, 8)]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Descending Order (High to Low):<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sorted_items = sorted(fruits.items(), key=lambda x: x[1], reverse=True)\n# Result: [('cherry', 8), ('apple', 5), ('date', 3), ('banana', 2)]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">sorted_items = <\/span><span style=\"color: #DCDCAA\">sorted<\/span><span style=\"color: #D4D4D4\">(fruits.items(), <\/span><span style=\"color: #9CDCFE\">key<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">lambda<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">: x[<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">], <\/span><span style=\"color: #9CDCFE\">reverse<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Result: [(&#39;cherry&#39;, 8), (&#39;apple&#39;, 5), (&#39;date&#39;, 3), (&#39;banana&#39;, 2)]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Convert the Sorted List Back to a Dictionary<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sorted_dict = dict(sorted_items)\nprint(sorted_dict)\n# Output (ascending): {'banana': 2, 'date': 3, 'apple': 5, 'cherry': 8}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">sorted_dict = <\/span><span style=\"color: #4EC9B0\">dict<\/span><span style=\"color: #D4D4D4\">(sorted_items)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(sorted_dict)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Output (ascending): {&#39;banana&#39;: 2, &#39;date&#39;: 3, &#39;apple&#39;: 5, &#39;cherry&#39;: 8}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Ascending: {'banana': 2, 'date': 3, 'apple': 5, 'cher ry': 8}\nDescending: {'cherry': 8, 'apple': 5, 'date': 3, 'banana': 2}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Ascending: {<\/span><span style=\"color: #CE9178\">&#39;banana&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;date&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;apple&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;cher ry&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Descending: {<\/span><span style=\"color: #CE9178\">&#39;cherry&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;apple&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;date&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;banana&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_Cases_for_Sorting_Dictionaries_by_Value\"><\/span>Use Cases for Sorting Dictionaries by Value<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s look at some examples of where you can benefit from sorting a dictionary by value.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Leaderboards in Games<\/strong>: Sort player scores to display rankings. <br>Example: <code>{\"Player1\": 1200, \"Player2\": 1500}<\/code> \u2192 Highest score first.<\/li>\n\n\n\n<li><strong>Word Frequency Analysis<\/strong>: Identify the most common words in a text.<br>Example: <code>{\"the\": 45, \"apple\": 12}<\/code> \u2192 Sort by counts.<\/li>\n\n\n\n<li><strong>Stock Price Tracking<\/strong>: Sort stocks by their current price.<br>Example: <code>{\"AAPL\": 180, \"GOOGL\": 2700}<\/code> \u2192 Cheapest first.<\/li>\n\n\n\n<li><strong>Student Grading<\/strong>: Rank students by exam scores.<br>Example: <code>{\"Alice\": 92, \"Bob\": 85}<\/code> \u2192 Highest grades first.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Key_Takeaways\"><\/span>Key Takeaways<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>sorted(dict.items(), key=lambda x: x[1])<\/code> to sort by values.<\/li>\n\n\n\n<li>Convert the sorted list back to a dictionary with <code>dict()<\/code>.<\/li>\n\n\n\n<li>Python 3.7+ preserves insertion order, so the sorted dictionary maintains the new order.<\/li>\n<\/ul>\n\n\n\n<p>By mastering this technique, you can efficiently organize data for analysis, reporting, or user-facing features! \ud83d\udc0d<\/p>\n\n\n\n<p><strong>Related Reading:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/metaschool.so\/articles\/python-switch-case\">Python Switch Case Guide: Using Match and Case Statements Simplified<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/python-string-searches\">Master String Searches in Python 2025<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/python-exponents\">Python Exponent: Complete Guide To Exponents in Python<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/python-check-if-file-exists\">How to check if a file exists in Python?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/what-is-floor-division\">What is Floor Division in Python? Guide With Examples<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":19,"featured_media":12426,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[342],"tags":[],"class_list":["post-12423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/12423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=12423"}],"version-history":[{"count":11,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/12423\/revisions"}],"predecessor-version":[{"id":12472,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/12423\/revisions\/12472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/12426"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=12423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=12423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=12423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}