{"id":89799,"date":"2023-03-02T05:29:48","date_gmt":"2023-03-02T05:29:48","guid":{"rendered":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/?p=89799"},"modified":"2026-05-13T08:15:12","modified_gmt":"2026-05-13T08:15:12","slug":"python-sort-dictionary-by-key-and-value-with-example","status":"publish","type":"post","link":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/","title":{"rendered":"Python Sort Dictionary by Key and Value with Example"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-what-are-dictionary-keys\" style=\"font-style:normal;font-weight:700\">What are dictionary keys?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>dictionary key<\/strong>\/s are from a Python object or part of an argument in a program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>Python dictionary is a collection of key-value pairs<\/strong> in which each key corresponds to a value. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A key-value pair&#8217;s value may be an integer, text, list, tuple, or even another dictionary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, you can use any acceptable Python type for the value of the key-value combination.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nevertheless, a dictionary key always has its corresponding value as you can see in actual dictionaries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-do-keys-do-in-python\" style=\"font-style:normal;font-weight:700\">What do KEYS () do in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python&#8217;s keys () function is used to retrieve all of the dictionary&#8217;s keys. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The keys must be unique and of an immutable type (string, integer, or tuple with immutable members).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Python key() function is different from the key that we call or sort from the dictionary. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The function of the key is specifically applicable to returning a key from the dictionary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, in some cases, programmers want to sort Python keys in a dictionary which is why we use other functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-sorting-needed-in-the-dictionary\" style=\"font-style:normal;font-weight:700\">Why Sorting Needed in the Dictionary?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sorting is very much needed in Python dictionary for the following reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sorting could help programmers handle large amounts of data in dictionaries.<\/li>\n\n\n\n<li>The sort() function enables Python programmers to create queries that sort data by value and keys.<\/li>\n\n\n\n<li> Sorting also reduces data complexity which makes the dictionary efficient and easy to read.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">We have the following ways how to sort Python dictionaries by key and values:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Sort keys <mark style=\"background-color:var(--base-3);color:#ff0000\" class=\"has-inline-color\">key_value.iterkeys()<\/mark>&nbsp;function.<\/li>\n\n\n\n<li>Sort keys through the <mark style=\"background-color:var(--base-3);color:#f00000\" class=\"has-inline-color\">(key_value)<\/mark>&nbsp;function. This function enables you to print the values of each key.<\/li>\n\n\n\n<li>The <mark style=\"background-color:var(--base-3);color:#ff0000\" class=\"has-inline-color\">key_value.iteritems()<\/mark> function is also able to sort data alphabetically.<\/li>\n\n\n\n<li>You may also use the <mark style=\"background-color:var(--base-3);color:#ff0000\" class=\"has-inline-color\">key = lambda (k, v) : (v, k))<\/mark> function to sort and display keys and values.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-important-things-you-need-to-do-to-sort-a-dictionary-in-python-by-value-and-key\" style=\"font-style:normal;font-weight:700\">Important things you need to do to sort a dictionary in Python by value and key<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first task is to create a dictionary and alphabetically display its list keys.<\/li>\n\n\n\n<li>The next thing is to display both the dictionary keys and values in the output. These keys and values must be sorted in alphabetical order.<\/li>\n\n\n\n<li>Lastly, you can try to sort the dictionary by keys or values and display it as output.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Sort Dictionary by key in Python<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following are the methods to sort <strong>dictionary by key<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-displaying-the-keys-in-sorted-order\" style=\"font-style:normal;font-weight:700\">1. Displaying the Keys in Sorted Order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we will try first how to sort the keys in a Python dictionary without displaying its values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def Dictionary():\n\tkeylists = {2: 4, 4: 8, 6: 12, 8: 16, 10: 20, 12: 24}\n\n\tfor i in sorted(keylists.keys()):\n\t\tprint(i, end=\" \")\n\n\ndef main():\n\tDictionary()\nmain()<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>2 4 6 8 10 12<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The output shows the sorted key from the Python dictionary. To sort it from the Python dictionary, the iterkeys() function is used. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is presented as <code>i<\/code> in the program and it returns all the keys from the given inputs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <mark style=\"background-color:var(--base-3);color:#ff0000\" class=\"has-inline-color\"><strong>for i in sorted(keylists.keys()):<\/strong><\/mark> is the function that really sorts the key from the Python dictionary. While <mark style=\"background-color:var(--base-3);color:#ff0000\" class=\"has-inline-color\"><strong>print(i, end=&#8221; &#8220;)<\/strong><\/mark> invokes the program to arrange the keys from lowest value to highest and displays the output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-sort-the-dictionary-by-key\" style=\"font-style:normal;font-weight:700\">2. Sort the dictionary by key<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This example will try to sort the key in the Python dictionary in lexicographic order which will display such as in actual dictionaries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import OrderedDict\n \ndict = {'Paul': '22', 'Prince': '24', 'Grace': '23', 'Gladys': '22'}\n\nprint(OrderedDict(sorted(dict.items())))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OrderedDict([('Gladys', '22'), ('Grace', '23'), ('Paul', '22'), ('Prince', '24')])<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the program sorts the Python keys (names) in the dictionary order. The outputs then display the keys along with their corresponding values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:var(--base-3);color:#ff0000\" class=\"has-inline-color\"><strong>OrderedDict<\/strong><\/mark> is a Python subclass under the built-in file &#8220;collections&#8221;. This subclass has the <strong><mark style=\"background-color:var(--base-3);color:#ff0606\" class=\"has-inline-color\">dict<\/mark> <\/strong>declaration which is understood as a Python dictionary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <mark style=\"background-color:var(--base-3);color:#ff0000\" class=\"has-inline-color\"><strong>print(OrderedDict(sorted(dict.items())))<\/strong><\/mark> enables the program to sort and display the keys under the subclass.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-sorting-the-keys-and-values-in-alphabetical-order\" style=\"font-style:normal;font-weight:700\">3. Sorting the Keys and Values in Alphabetical Order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The next example will try to show you how to sort Python keys and values in a dictionary. This will <strong>sort the keys<\/strong> in alphabetical order along with their values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def Dictionary():\n    \n    keylists = {2: 24, 4: 12, 6: 12, 8: 10, 10: 2, 12: 8}\n    \n    for i in sorted(keylists):\n        print((i, keylists&#91;i]), end=\" \")\n\ndef main():\n    Dictionary()\nmain()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(2, 24) (4, 12) (6, 12) (8, 10) (10, 2) (12, 8)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The outputs justify how the function sorts the keys in the Python dictionary. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not literally mean alphabetically arranged but the concept is it displays the keys in order (lowest to highest) and their values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The functions of each program line were the same as the explanation in the first example. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The only difference is their outputs wherein the output of the former example does not include the values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-sorting-the-keys-and-values-in-alphabetical-using-the-value\" style=\"font-style:normal;font-weight:700\">4. Sorting the Keys and Values in alphabetical using the value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This example will now show how to <strong>sort keys based on their values<\/strong> within the Python dictionary. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Basically, the output should be based on the values of each key in the form of lowest to highest order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def Dictionary():\n    \n    keylists = {2: 24, 4: 12, 6: 12, 8: 10, 10: 2, 12: 8}\n    \n    print(sorted(keylists.items(), key=lambda kv:\n                 (kv&#91;1], kv&#91;0])))\n\ndef main():\n    Dictionary()\nmain()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[(10, 2), (12, 8), (8, 10), (4, 12), (6, 12), (2, 24)]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you see in the output, this shows the lowest to highest order of the dictionary based on its values. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When this function is applied to strings, it will show an alphabetical order of strings as an output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-sort-dictionary-by-value-in-python\" style=\"font-style:normal;font-weight:700\">5. Sort Dictionary By Value in Python<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example below shows how the function works with string as key and integers as their values. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will sort the Python keys within a dictionary from lowest to highest (alphabetical) based on their values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This means that the output should display a series of dictionaries with their values arranged from lowest to highest.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy\nfrom collections import OrderedDict\n \ndict = {'Paul': '22', 'Prince': '24', 'Grace': '23', 'Gladys': '22'}\n\nsortedValue = numpy.argsort(dict.values())\nkeys = list(dict.keys())\nsortedDict = {keys&#91;i]: sorted(\n    dict.values())&#91;i] for i in range(len(keys))}\n \nprint(sortedDict)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OrderedDict([('Gladys', '22'), ('Paul', '22'), ('Grace', '23'), ('Prince', '24')])<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The output proves that the keys were sorted according to their values. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Their values were arranged from lowest to highest, therefore the keys were placed when their value belonged.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary\" style=\"font-style:normal;font-weight:700\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In summary, we covered every detail of the topic of how to do Python sort dictionary by key and values with complete discussion and examples.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Furthermore, you also discovered the important task of how to perform the Python sort dictionary by key and by values. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may also try the given discussion and tutorials on how to implement sorting Python dictionaries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But if you want to clarify any of the discussed terms or topics, you can always keep us informed through the comments.<\/p>\n\n\n\n<h2 style=\"color:#1F3A5F;border-left:4px solid #C9A961;padding-left:12px;margin-top:2em;\">Related Python Tutorials<\/h2><ul style=\"line-height:2;color:#2c3e50;\"><li><a href=\"\/https\/itsourcecode.com\/python-tutorial\/python-print-dictionary-with-examples\/\">Python Print Dictionary With Examples<\/a><\/li><li><a href=\"\/https\/itsourcecode.com\/python-tutorial\/python-copy-a-dictionary-with-best-example\/\">Python Copy A Dictionary With Best Example<\/a><\/li><li><a href=\"\/https\/itsourcecode.com\/python-tutorial\/solved-how-to-increment-value-in-dictionary-python\/\">Solved How To Increment Value In Dictionary Python<\/a><\/li><li><a href=\"\/https\/itsourcecode.com\/python-tutorial\/is-dictionary-mutable-in-python\/\">Is Dictionary Mutable In Python<\/a><\/li><li><a href=\"\/https\/itsourcecode.com\/python-tutorial\/how-to-add-to-a-dictionary-in-python\/\">How To Add To A Dictionary In Python<\/a><\/li><li><a href=\"\/https\/itsourcecode.com\/python-tutorial\/difference-between-list-and-dictionary-in-python\/\">Difference Between List And Dictionary In Python<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What are dictionary keys? The dictionary key\/s are from a Python object or part of an argument in a program. A Python dictionary is a collection of key-value pairs in &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python Sort Dictionary by Key and Value with Example\" class=\"read-more button\" href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#more-89799\" aria-label=\"Read more about Python Sort Dictionary by Key and Value with Example\">Read more<\/a><\/p>\n","protected":false},"author":2351,"featured_media":92624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61036],"tags":[96637,96638,96639],"class_list":["post-89799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-how-to-sort-python-dictionary-by-key","tag-sort-dictionary-in-python-by-key-and-values","tag-sort-dictionary-in-python-by-key-with-examples","resize-featured-image"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v27.8) - https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Sort Dictionary by Key and Value with Example - Itsourcecode.com<\/title>\n<meta name=\"description\" content=\"This full tutorial about Python sort dictionary by key and value with examples will give you not only knowledge but also skills on how...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Sort Dictionary by Key and Value with Example\" \/>\n<meta property=\"og:description\" content=\"This full tutorial about Python sort dictionary by key and value with examples will give you not only knowledge but also skills on how...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Itsourcecode.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-02T05:29:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-13T08:15:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2023\/02\/Python-Sort-Dictionary.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1460\" \/>\n\t<meta property=\"og:image:height\" content=\"900\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Elijah Galero\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Elijah Galero\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/\"},\"author\":{\"name\":\"Elijah Galero\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/e7dc09b7311b1480f47d9bc38f5f0ad8\"},\"headline\":\"Python Sort Dictionary by Key and Value with Example\",\"datePublished\":\"2023-03-02T05:29:48+00:00\",\"dateModified\":\"2026-05-13T08:15:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/\"},\"wordCount\":1020,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/3883cf6bf7d0f141f81ccef0de9dd3fd\"},\"image\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/Python-Sort-Dictionary.png\",\"keywords\":[\"how to sort Python dictionary by key?\",\"sort dictionary in Python by key and values\",\"sort dictionary in Python by key with examples\"],\"articleSection\":[\"Python Tutorial 2026 \u2014 Free Beginner to Advanced Guide\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/\",\"name\":\"Python Sort Dictionary by Key and Value with Example - Itsourcecode.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/Python-Sort-Dictionary.png\",\"datePublished\":\"2023-03-02T05:29:48+00:00\",\"dateModified\":\"2026-05-13T08:15:12+00:00\",\"description\":\"This full tutorial about Python sort dictionary by key and value with examples will give you not only knowledge but also skills on how...\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#primaryimage\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/Python-Sort-Dictionary.png\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/Python-Sort-Dictionary.png\",\"width\":1460,\"height\":900,\"caption\":\"Python Sort Dictionary\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-sort-dictionary-by-key-and-value-with-example\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/itsourcecode.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Sort Dictionary by Key and Value with Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#website\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/\",\"name\":\"Itsourcecode.com\",\"description\":\"Partner In Your Coding Journey!\",\"publisher\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/3883cf6bf7d0f141f81ccef0de9dd3fd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/itsourcecode.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/3883cf6bf7d0f141f81ccef0de9dd3fd\",\"name\":\"itsourcecode\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"width\":409,\"height\":409,\"caption\":\"itsourcecode\"},\"logo\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\"},\"description\":\"Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!\",\"sameAs\":[\"https:\\\/\\\/itsourcecode.com\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/e7dc09b7311b1480f47d9bc38f5f0ad8\",\"name\":\"Elijah Galero\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e7574d00f88d6811f85ae83776381fe5.jpg?ver=1781886580\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e7574d00f88d6811f85ae83776381fe5.jpg?ver=1781886580\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e7574d00f88d6811f85ae83776381fe5.jpg?ver=1781886580\",\"caption\":\"Elijah Galero\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Sort Dictionary by Key and Value with Example - Itsourcecode.com","description":"This full tutorial about Python sort dictionary by key and value with examples will give you not only knowledge but also skills on how...","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:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/","og_locale":"en_US","og_type":"article","og_title":"Python Sort Dictionary by Key and Value with Example","og_description":"This full tutorial about Python sort dictionary by key and value with examples will give you not only knowledge but also skills on how...","og_url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/","og_site_name":"Itsourcecode.com","article_published_time":"2023-03-02T05:29:48+00:00","article_modified_time":"2026-05-13T08:15:12+00:00","og_image":[{"width":1460,"height":900,"url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2023\/02\/Python-Sort-Dictionary.png","type":"image\/png"}],"author":"Elijah Galero","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Elijah Galero","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#article","isPartOf":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/"},"author":{"name":"Elijah Galero","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/e7dc09b7311b1480f47d9bc38f5f0ad8"},"headline":"Python Sort Dictionary by Key and Value with Example","datePublished":"2023-03-02T05:29:48+00:00","dateModified":"2026-05-13T08:15:12+00:00","mainEntityOfPage":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/"},"wordCount":1020,"commentCount":0,"publisher":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/3883cf6bf7d0f141f81ccef0de9dd3fd"},"image":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#primaryimage"},"thumbnailUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2023\/02\/Python-Sort-Dictionary.png","keywords":["how to sort Python dictionary by key?","sort dictionary in Python by key and values","sort dictionary in Python by key with examples"],"articleSection":["Python Tutorial 2026 \u2014 Free Beginner to Advanced Guide"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/","name":"Python Sort Dictionary by Key and Value with Example - Itsourcecode.com","isPartOf":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#primaryimage"},"image":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#primaryimage"},"thumbnailUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2023\/02\/Python-Sort-Dictionary.png","datePublished":"2023-03-02T05:29:48+00:00","dateModified":"2026-05-13T08:15:12+00:00","description":"This full tutorial about Python sort dictionary by key and value with examples will give you not only knowledge but also skills on how...","breadcrumb":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#primaryimage","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2023\/02\/Python-Sort-Dictionary.png","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2023\/02\/Python-Sort-Dictionary.png","width":1460,"height":900,"caption":"Python Sort Dictionary"},{"@type":"BreadcrumbList","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-tutorial\/python-sort-dictionary-by-key-and-value-with-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/"},{"@type":"ListItem","position":2,"name":"Python Sort Dictionary by Key and Value with Example"}]},{"@type":"WebSite","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#website","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/","name":"Itsourcecode.com","description":"Partner In Your Coding Journey!","publisher":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/3883cf6bf7d0f141f81ccef0de9dd3fd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/3883cf6bf7d0f141f81ccef0de9dd3fd","name":"itsourcecode","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","width":409,"height":409,"caption":"itsourcecode"},"logo":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg"},"description":"Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!","sameAs":["https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/"]},{"@type":"Person","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/e7dc09b7311b1480f47d9bc38f5f0ad8","name":"Elijah Galero","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/litespeed\/avatar\/e7574d00f88d6811f85ae83776381fe5.jpg?ver=1781886580","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/litespeed\/avatar\/e7574d00f88d6811f85ae83776381fe5.jpg?ver=1781886580","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/litespeed\/avatar\/e7574d00f88d6811f85ae83776381fe5.jpg?ver=1781886580","caption":"Elijah Galero"}}]}},"_links":{"self":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/89799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/users\/2351"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/comments?post=89799"}],"version-history":[{"count":8,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/89799\/revisions"}],"predecessor-version":[{"id":129512,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/89799\/revisions\/129512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/media\/92624"}],"wp:attachment":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/media?parent=89799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/categories?post=89799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/tags?post=89799"}],"curies":[{"name":"wp","href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}