{"id":10616,"date":"2021-04-19T18:20:07","date_gmt":"2021-04-19T12:50:07","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=10616"},"modified":"2021-04-19T18:22:45","modified_gmt":"2021-04-19T12:52:45","slug":"sort-dictionary-by-value-python","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/","title":{"rendered":"How to Sort a Dictionary by Value in Python"},"content":{"rendered":"\n<p>In python, we have discussed many concepts on Dictionary. But sometimes, we come to a situation where we need to sort the dictionary by its value. In this tutorial, we will be discussing how to <strong>sort a dictionary by value<\/strong> in python. <\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#What_is_dictionary\" >What is dictionary?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#What_is_sorting\" >What is sorting?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#Need_of_sorting_a_dictionary\" >Need of sorting a dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#5_Ways_to_sort_a_dictionary_by_value_in_Python\" >5 Ways to sort a dictionary by value in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#1_Using_a_sorted_with_operator_module\" >1. Using a sorted() with operator module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#2_Using_sorted_function_with_lambda_function_to_sort_a_dictionary_by_value_in_Python\" >2. Using sorted() function with lambda() function to sort a dictionary by value in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#3_Using_sorted_function_with_dictitems_to_sort_a_dictionary_by_value_in_Python\" >3. Using sorted() function with dict.items() to sort a dictionary by value in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#4_Using_sorted_function_and_return_an_ordered_dictionary\" >4. Using sorted() function and return an ordered dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#5_Using_for_Loop_to_sort_a_dictionary_by_value_in_Python\" >5. Using for Loop to sort a dictionary by value in Python<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-dictionary\"><span class=\"ez-toc-section\" id=\"What_is_dictionary\"><\/span>What is dictionary?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Dictionary&nbsp;in Python is an unordered collection of data values, used to store the data values like a map, unlike other Data Types that hold only a single value as an element.<\/strong> It holds the <strong>key: value pair, <\/strong>which is provided<strong> <\/strong>to make the dictionary more optimized. Dictionary is mutable in nature, which says that changes can be done after declaring it. It allows duplicate entries. We declare dictionary in curly braces { }.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-sorting\"><span class=\"ez-toc-section\" id=\"What_is_sorting\"><\/span>What is sorting?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Sorting means sequentially arranging the values<\/strong>,<strong> like ascending order or descending order. <\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-need-of-sorting-a-dictionary\"><span class=\"ez-toc-section\" id=\"Need_of_sorting_a_dictionary\"><\/span>Need of sorting a dictionary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>A <strong>dictionary has an O(1) search time complexity<\/strong> which is best in comparison between list, tuple, etc.<\/li><li>A <a href=\"http:\/\/www.pythonpool.com\/sort-dictionary-by-key-in-python\/\" target=\"_blank\" rel=\"noopener\">sorted dictionary<\/a> gives a better understanding of the concepts and clarity in handling the operations.<\/li><li>Sorting helps us to make efficient analyses while we are working with any data structure.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-5-ways-to-sort-a-dictionary-by-value-in-python\"><span class=\"ez-toc-section\" id=\"5_Ways_to_sort_a_dictionary_by_value_in_Python\"><\/span>5 Ways to sort a dictionary by value in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here, we will be discussing all the different ways through which we can sort a dictionary by value: <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-using-a-sorted-with-operator-module\"><span class=\"ez-toc-section\" id=\"1_Using_a_sorted_with_operator_module\"><\/span>1. Using a sorted() with operator module<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be using the sorted() function and applying the itemgetter() function from the operator module. <strong>This function will sort the dictionary in the form of a list of tuples which will be sorted according to the second element in the tuple.<\/strong> After that, we will be applying the dict() function to convert it again into the dictionary. If you want to sort a dictionary in descending order, you need to write reverse = True inside the function. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import operator module\nimport operator\n\ndic = {'python' : 3, 'pool' : 5, 'solution' : 6, 'sid' : 1}\nprint(&quot;Input dictionary : &quot;,dic)\nsort = sorted(dic.items(), key=operator.itemgetter(1))\n\noutput = dict(sort)\nprint(&quot;Output sorted dictionary : &quot;,output)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"> <strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input dictionary :  {'python': 3, 'pool': 5, 'solution': 6, 'sid': 1}\nOutput sorted dictionary :  {'sid': 1, 'python': 3, 'pool': 5, 'solution': 6}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Firstly, we will import the operator module.<\/li><li>Then, we will take the input dictionary in the variable named dic.<\/li><li>After that, we have printed the input dictionary.<\/li><li>Then, we have applied the sorted function inside which we have given two parameters as the values of the dictionary and the key = operator.itemgetter(1). Here, 1 represents the values of the dict, and 0 represents the values of the keys.<\/li><li>Then, we have converted the list of tuples into the dictionary with the help dict function and stored the output in the output variable.<\/li><li>Hence, you can see the sorted dictionary.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-using-sorted-function-with-lambda-function-to-sort-a-dictionary-by-value-in-python\"><span class=\"ez-toc-section\" id=\"2_Using_sorted_function_with_lambda_function_to_sort_a_dictionary_by_value_in_Python\"><\/span>2. Using sorted() function with lambda() function to sort a dictionary by value in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be using the sorted() function, which will be clubbed with the lambda() function, which will sort the dictionary by value. <strong>Lambda() function is used to create the anonymous function, i.e., a function without the name.<\/strong> It will help to optimize the code. If you want to sort a dictionary in descending order, you need to write reverse = True inside the function. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndic = {'python' : 3, 'pool' : 5, 'solution' : 6, 'sid' : 1}\nprint(&quot;Input dictionary : &quot;,dic)\n\nsort = sorted(dic.items(), key=lambda item: item&#x5B;1])\noutput = dict(sort)\nprint(&quot;Output sorted dictionary : &quot;,output)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input dictionary :  {'python': 3, 'pool': 5, 'solution': 6, 'sid': 1}\nOutput sorted dictionary :  {'sid': 1, 'python': 3, 'pool': 5, 'solution': 6}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-52202957-bf42-4aad-b795-18e9e8e66390\"><li>Firstly, we will take the input dictionary in the variable named dic.<\/li><li>After that, we have printed the input dictionary.<\/li><li>Then, we have applied the sorted() function with both the parameters as dic.items() and key = lambda item: item[1]. in this, also 1 is treated for the values of the input dictionary.<\/li><li>Then, we have converted the list of tuples into the dictionary with the help dict function and stored the output in the output variable.<\/li><li>Hence, you can see the sorted dictionary.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-using-sorted-function-with-dict-items-to-sort-a-dictionary-by-value-in-python\"><span class=\"ez-toc-section\" id=\"3_Using_sorted_function_with_dictitems_to_sort_a_dictionary_by_value_in_Python\"><\/span>3. Using sorted() function with dict.items() to sort a dictionary by value in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be using the sorted() function, which will be clubbed with <a href=\"http:\/\/www.pythonpool.com\/python-iteritems\/\" target=\"_blank\" rel=\"noreferrer noopener\">dict.items() function<\/a>. <strong>Dict.items() function is used to fetch the keys\/values from a dictionary.<\/strong> If you want to sort a dictionary in descending order, you need to write reverse = True inside the function. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndic = {'python' : 3, 'pool' : 5, 'solution' : 6, 'sid' : 1}\nprint(&quot;Input dictionary : &quot;,dic)\n\noutput = dict(sorted((value, key) for (key,value) in dic.items()))\nprint(&quot;Output sorted dictionary : &quot;,output)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input dictionary :  {'python': 3, 'pool': 5, 'solution': 6, 'sid': 1}\nOutput sorted dictionary :  {1: 'sid', 3: 'python', 5: 'pool', 6: 'solution'}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Firstly, we will take the input dictionary in the variable named dic.<\/li><li>After that, we have printed the input dictionary.<\/li><li>Then, we have applied the dict() function for converting the list of tuples into the dictionary.<\/li><li>Inside the dict() function, we have applied the sorted() function, and inside it, we have applied all the required parameters.<\/li><li>Hence, you can see the sorted dictionary.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-using-sorted-function-and-return-an-ordered-dictionary\"><span class=\"ez-toc-section\" id=\"4_Using_sorted_function_and_return_an_ordered_dictionary\"><\/span>4. Using sorted() function and return an ordered dictionary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will import orderedDict from the collections module. Then, we will use orderedDict() function clubbed with the sorted() function. If you want to sort a dictionary in descending order, you need to write reverse = True inside the function. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import OrderedDict from collections module\nfrom collections import OrderedDict\n\ndic = {'python' : 3, 'pool' : 5, 'solution' : 6, 'sid' : 1}\nprint(&quot;Input dictionary : &quot;,dic)\n\noutput = OrderedDict(sorted(dic.items(), key=lambda x: x&#x5B;1]))\nprint(&quot;Sorted dictionary : &quot;,output)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input dictionary :  {'python': 3, 'pool': 5, 'solution': 6, 'sid': 1}\nSorted dictionary :  OrderedDict(&#91;('sid', 1), ('python', 3), ('pool', 5), ('solution', 6)])<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-52202957-bf42-4aad-b795-18e9e8e66390\"><li>Firstly, we will import the OrderedDict function from the collections module.<\/li><li>Then, we will take the input dictionary in the variable named dic.<\/li><li>After that, we have printed the input dictionary.<\/li><li>After that, we have applied the orderedDict() function inside which we have applied the sorted() function.<\/li><li>Inside, which we have applied two parameters as dic.items() and lambda function.<\/li><li>Hence, you can see the sorted dictionary as the output.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-using-for-loop-to-sort-a-dictionary-by-value-in-python\"><span class=\"ez-toc-section\" id=\"5_Using_for_Loop_to_sort_a_dictionary_by_value_in_Python\"><\/span>5. Using for Loop to sort a dictionary by value in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be sorting a dictionary by using the for loop. If you want to sort a dictionary in descending order, you need to write reverse = True inside the function. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndic = {'python' : 3, 'pool' : 5, 'solution' : 6, 'sid' : 1}\nprint(&quot;Input dictionary : &quot;,dic)\n\nsorted_values = sorted(dic.values()) # Sort the values\nsorted_dict = {}\n\nfor i in sorted_values:\n    for k in dic.keys():\n        if dic&#x5B;k] == i:\n            sorted_dict&#x5B;k] = dic&#x5B;k]\n            break\n\nprint(&quot;Sorted dictionary : &quot;,sorted_dict)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input dictionary :  {'python': 3, 'pool': 5, 'solution': 6, 'sid': 1}\nSorted dictionary :  {'sid': 1, 'python': 3, 'pool': 5, 'solution': 6}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-3c5909ce-343c-4825-8fc0-6cab04f1c9c8\"><li>Firstly, we will take the input dictionary in the variable named dic.<\/li><li>After that, we have printed the input dictionary.<\/li><li>Then, we have sorted the values of the input dictionary with the help of the sorted function.<\/li><li>We have taken an empty dictionary with the name sorted_dict{ }.<\/li><li>After that, we have applied for loop up to the range of sorted_values.<\/li><li>Inside it again a for loop and inside an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Conditional_(computer_programming)#If%E2%80%93then(%E2%80%93else)\" target=\"_blank\" rel=\"noreferrer noopener\">if condition<\/a>, which will check the condition and store the output in the empty dictionary.<\/li><li>At last, we have printed the empty dictionary.<\/li><li>Hence, you can see the sorted dictionary as the output.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this tutorial, we have learned about the concept of the sort a dictionary by value. We have seen what a dictionary is and what is sorting. We have also discussed what the need for sorting the dictionary is. After that, we have discussed all the through which we can sort the dictionary by value. All the ways are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.<\/p>\n\n\n\n<p>However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In python, we have discussed many concepts on Dictionary. But sometimes, we come to a situation where we need to sort the dictionary by its &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Sort a Dictionary by Value in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#more-10616\" aria-label=\"More on How to Sort a Dictionary by Value in Python\">Read more<\/a><\/p>\n","protected":false},"author":17,"featured_media":10687,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[3866,3870,3867,3868,3869],"class_list":["post-10616","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-python-dictionary-sort-by-value","tag-python-sort-a-dictionary-by-value","tag-python-sort-dictionary-by-value","tag-sort-a-dictionary-by-value-python","tag-sort-dictionary-by-value-python-3","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Sort a Dictionary by Value in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"Sort a dictionary by value in Python through multiple ways like for loop, lambda() function, operator module, dict.items() function, etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Sort a Dictionary by Value in Python\" \/>\n<meta property=\"og:description\" content=\"In python, we have discussed many concepts on Dictionary. But sometimes, we come to a situation where we need to sort the dictionary by its value. In this\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-19T12:50:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-19T12:52:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Siddharth Jain\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Siddharth Jain\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/\"},\"author\":{\"name\":\"Siddharth Jain\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa\"},\"headline\":\"How to Sort a Dictionary by Value in Python\",\"datePublished\":\"2021-04-19T12:50:07+00:00\",\"dateModified\":\"2021-04-19T12:52:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/\"},\"wordCount\":1144,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg\",\"keywords\":[\"python dictionary sort by value\",\"python sort a dictionary by value\",\"python sort dictionary by value\",\"sort a dictionary by value python\",\"sort dictionary by value python 3\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/\",\"url\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/\",\"name\":\"How to Sort a Dictionary by Value in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg\",\"datePublished\":\"2021-04-19T12:50:07+00:00\",\"dateModified\":\"2021-04-19T12:52:45+00:00\",\"description\":\"Sort a dictionary by value in Python through multiple ways like for loop, lambda() function, operator module, dict.items() function, etc.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg\",\"width\":1350,\"height\":650,\"caption\":\"sort dictionary by value python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Sort a Dictionary by Value in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa\",\"name\":\"Siddharth Jain\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"caption\":\"Siddharth Jain\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Sort a Dictionary by Value in Python - Python Pool","description":"Sort a dictionary by value in Python through multiple ways like for loop, lambda() function, operator module, dict.items() function, etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Sort a Dictionary by Value in Python","og_description":"In python, we have discussed many concepts on Dictionary. But sometimes, we come to a situation where we need to sort the dictionary by its value. In this","og_url":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/","og_site_name":"Python Pool","article_published_time":"2021-04-19T12:50:07+00:00","article_modified_time":"2021-04-19T12:52:45+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg","type":"image\/jpeg"}],"author":"Siddharth Jain","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Siddharth Jain","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/"},"author":{"name":"Siddharth Jain","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa"},"headline":"How to Sort a Dictionary by Value in Python","datePublished":"2021-04-19T12:50:07+00:00","dateModified":"2021-04-19T12:52:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/"},"wordCount":1144,"commentCount":4,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg","keywords":["python dictionary sort by value","python sort a dictionary by value","python sort dictionary by value","sort a dictionary by value python","sort dictionary by value python 3"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/","url":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/","name":"How to Sort a Dictionary by Value in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg","datePublished":"2021-04-19T12:50:07+00:00","dateModified":"2021-04-19T12:52:45+00:00","description":"Sort a dictionary by value in Python through multiple ways like for loop, lambda() function, operator module, dict.items() function, etc.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Sort-a-Dictionary-by-Value-in-Python.jpg","width":1350,"height":650,"caption":"sort dictionary by value python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/sort-dictionary-by-value-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"How to Sort a Dictionary by Value in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa","name":"Siddharth Jain","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","caption":"Siddharth Jain"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=10616"}],"version-history":[{"count":9,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10616\/revisions"}],"predecessor-version":[{"id":24381,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10616\/revisions\/24381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/10687"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=10616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=10616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=10616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}