{"id":3863,"date":"2020-08-10T12:59:30","date_gmt":"2020-08-10T07:29:30","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3863"},"modified":"2026-07-13T12:27:50","modified_gmt":"2026-07-13T06:57:50","slug":"python-sort-list-of-lists","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/","title":{"rendered":"Python Sort List of Lists: key, itemgetter, and Multiple Keys"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> Sort a list of lists by supplying a key that extracts or computes the value to compare. Use sorted() for a new result, list.sort() for intentional in-place mutation, and tuple keys for multiple ordering fields. Validate inner-list shape and types before sorting external data.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-b092.png\" alt=\"Python Pool infographic showing Python list of lists sorted key itemgetter reverse length sum and stable sorting\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>Python sorting is driven by a key: choose the inner position or derived value, then decide whether the original list should change.<\/figcaption><\/figure>\n<p>Sorting a list of lists usually means keeping each inner list together while choosing one part of that row as the sort key. Python handles this cleanly with <code>sorted()<\/code>, <code>list.sort()<\/code>, <code>key=<\/code>, lambda functions, and <code>operator.itemgetter()<\/code>.<\/p>\n<p>The official Python documentation explains the <a href=\"https:\/\/docs.python.org\/3\/howto\/sorting.html\">Sorting HOWTO<\/a>, <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#list.sort\">list.sort()<\/a>, and <a href=\"https:\/\/docs.python.org\/3\/library\/operator.html#operator.itemgetter\">operator.itemgetter()<\/a>.<\/p>\n<p>Use <code>sorted(rows, key=...)<\/code> when you want a new sorted list and need to keep the original list unchanged. Use <code>rows.sort(key=...)<\/code> when it is fine to reorder the existing list in place.<\/p>\n<p>The <code>key<\/code> function receives each inner list and returns the value Python should compare. For example, <code>lambda row: row[1]<\/code> sorts by the second item in each row.<\/p>\n<p>Python sorting is stable. If two rows have the same key, their original order is preserved. Stability is useful when you sort in multiple passes or when equal values should keep a meaningful input order.<\/p>\n<p>Before sorting, confirm that every inner list has the position your key reads. If some rows are shorter, a position-based key can raise <code>IndexError<\/code>. Validate messy input or write a key that handles missing fields.<\/p>\n<p>For simple item positions, <code>itemgetter()<\/code> is concise and fast. For expressions such as sums, lengths, combined keys, or sign changes, a lambda is usually clearer.<\/p>\n<p>Use <code>reverse=True<\/code> for descending order. Do not reverse the final list manually unless there is a specific reason; the built-in argument states the intent directly.<\/p>\n<p>Keep sort keys simple and cheap. Python calls the key function once for each row, stores the key internally, and then sorts using those stored keys. That makes key-based sorting efficient, but a key that performs slow work can still make the whole sort feel slow.<\/p>\n<p>Be consistent about row shape. If index <code>0<\/code> means a name in one row and a category in another row, the sort may run but the result will be hard to trust. Clean the data before sorting when rows come from user input, CSV files, or mixed sources.<\/p>\n<p>If the inner lists represent long records, consider switching to dictionaries, tuples with named positions, dataclasses, or small objects. Lists are fine for compact examples, but named fields are easier to maintain when the row has many columns.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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\/python-sort-list-of-lists\/#Sort_By_One_Position\" >Sort By One Position<\/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\/python-sort-list-of-lists\/#Sort_In_Place_With_itemgetter\" >Sort In Place With itemgetter<\/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\/python-sort-list-of-lists\/#Sort_In_Reverse_Order\" >Sort In Reverse Order<\/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\/python-sort-list-of-lists\/#Sort_By_Inner_List_Length\" >Sort By Inner List Length<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#Sort_By_The_Sum_Of_Each_Row\" >Sort By The Sum Of Each Row<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#Sort_By_Multiple_Keys\" >Sort By Multiple Keys<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#Use_A_Key_Function\" >Use A Key Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#Choose_New_Or_In-Place_Results\" >Choose New Or In-Place Results<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#Sort_By_Derived_Values\" >Sort By Derived Values<\/a><\/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\/python-sort-list-of-lists\/#Use_Tuple_Keys_For_Tie-Breaking\" >Use Tuple Keys For Tie-Breaking<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#Test_Shape_Types_And_Stability\" >Test Shape, Types, And Stability<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#How_do_I_sort_a_list_of_lists_by_the_first_item\" >How do I sort a list of lists by the first item?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#What_is_the_difference_between_sorted_and_listsort\" >What is the difference between sorted() and list.sort()?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#How_do_I_sort_by_multiple_values\" >How do I sort by multiple values?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#How_do_I_sort_lists_of_different_lengths_safely\" >How do I sort lists of different lengths safely?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sort_By_One_Position\"><\/span>Sort By One Position<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use a key function to choose which item in each row controls the order.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">rows = [[\"Ada\", 90], [\"Linus\", 85], [\"Guido\", 95]]\n\nresult = sorted(rows, key=lambda row: row[1])\n\nprint(result)\n<\/code><\/pre>\n<\/div>\n<p>The rows are sorted by the score at index <code>1<\/code>.<\/p>\n<p>The original rows stay intact.<\/p>\n<p><code>sorted()<\/code> returns a new list, so the original <code>rows<\/code> list is still available in its first order.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sort_In_Place_With_itemgetter\"><\/span>Sort In Place With itemgetter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>list.sort()<\/code> when the existing list should be reordered.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from operator import itemgetter\n\nrows = [[\"red\", 3], [\"blue\", 1], [\"green\", 2]]\nrows.sort(key=itemgetter(1))\n\nprint(rows)\n<\/code><\/pre>\n<\/div>\n<p><code>itemgetter(1)<\/code> selects the second item in each inner list.<\/p>\n<p>The list is changed in place.<\/p>\n<p>This form is compact when you only need to sort by one item position.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/sort-lists-key-b211.png\" alt=\"Python Pool infographic showing lists, key function, comparison field, and sorted output\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>A key function selects the nested-list value used for ordering.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Sort_In_Reverse_Order\"><\/span>Sort In Reverse Order<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>reverse=True<\/code> for descending order.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">rows = [[\"Ada\", 90], [\"Linus\", 85], [\"Guido\", 95]]\n\nresult = sorted(rows, key=lambda row: row[1], reverse=True)\n\nprint(result)\n<\/code><\/pre>\n<\/div>\n<p>The highest score appears first.<\/p>\n<p>The key still reads index <code>1<\/code>; only the final comparison direction changes.<\/p>\n<p>This is clearer than sorting ascending and then reversing the list in a separate step.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sort_By_Inner_List_Length\"><\/span>Sort By Inner List Length<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>len<\/code> as the key when row length decides the order.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">rows = [[\"a\", \"b\", \"c\"], [\"x\"], [\"m\", \"n\"]]\n\nresult = sorted(rows, key=len)\n\nprint(result)\n<\/code><\/pre>\n<\/div>\n<p>The shortest inner list comes first.<\/p>\n<p>This can be useful when processing rows with optional fields or grouped values of different sizes.<\/p>\n<p>Because <code>len<\/code> is already a function, it can be passed directly as the key.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/sort-lists-itemgetter-b211.png\" alt=\"Python Pool infographic mapping nested lists through itemgetter to sorted records\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>itemgetter is a concise key function when the sort field is a known index.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Sort_By_The_Sum_Of_Each_Row\"><\/span>Sort By The Sum Of Each Row<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>sum<\/code> as the key for rows that contain only numbers.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">scores = [[8, 7, 9], [10, 6, 5], [6, 6, 6]]\n\nresult = sorted(scores, key=sum)\n\nprint(result)\n<\/code><\/pre>\n<\/div>\n<p>The row with the smallest total appears first.<\/p>\n<p>This works when every inner list contains values that can be added together.<\/p>\n<p>If rows mix text and numbers, write a custom key that selects only the numeric fields.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sort_By_Multiple_Keys\"><\/span>Sort By Multiple Keys<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Return a tuple from the key function when more than one field matters.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">rows = [[\"sales\", \"Amy\", 3], [\"sales\", \"Bo\", 5], [\"ops\", \"Cy\", 4]]\n\nresult = sorted(rows, key=lambda row: (row[0], -row[2]))\n\nprint(result)\n<\/code><\/pre>\n<\/div>\n<p>This sorts by department first.<\/p>\n<p>Inside each department, the negative score places larger scores before smaller ones.<\/p>\n<p>Tuple keys are a reliable way to express primary and secondary sort rules in one pass.<\/p>\n<p>In short, use <code>sorted()<\/code> for a new list, <code>list.sort()<\/code> for in-place sorting, <code>key=<\/code> to choose the row field or expression, <code>itemgetter()<\/code> for simple item positions, <code>reverse=True<\/code> for descending order, and tuple keys for multi-column sorting.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_A_Key_Function\"><\/span>Use A Key Function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>key=lambda row: row[index] makes the sort criterion explicit, while operator.itemgetter(index) is a concise alternative for a fixed position. The key is evaluated for comparison and does not need to mutate the rows. Name the key or wrap it in a function when the rule has business meaning.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/sort-lists-multi-b211.png\" alt=\"Python Pool infographic comparing primary key, secondary key, tuple key, and stable output\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Return a tuple or use stable multi-pass sorting for compound ordering rules.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Choose_New_Or_In-Place_Results\"><\/span>Choose New Or In-Place Results<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>sorted(rows, key=&#8230;) returns a new list and leaves the original available for comparison or reuse. rows.sort(key=&#8230;) changes the list in place and returns None. Pick one deliberately, especially when aliases or cached references may observe the same list.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sort_By_Derived_Values\"><\/span>Sort By Derived Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A key can compute len(row), sum(row), a normalized string, or another safe value. Validate empty rows and mixed types before using a derived operation, and decide whether numeric conversion or missing values belong in the key or in an earlier data-cleaning step.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/sort-lists-check-b211.png\" alt=\"Python Pool infographic testing missing fields, mixed types, reverse order, and validation\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Check missing fields, comparable types, ties, reverse ordering, and input mutation.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Tuple_Keys_For_Tie-Breaking\"><\/span>Use Tuple Keys For Tie-Breaking<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Return (row[0], row[1]) to sort by a primary and secondary field. Python&#8217;s sort is stable, so equal keys retain their input order. If different fields need different directions, transform the relevant key or use a staged stable sort with a documented order.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Test_Shape_Types_And_Stability\"><\/span>Test Shape, Types, And Stability<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Test duplicates, empty lists, missing positions, mixed numeric and text data, reverse order, and already sorted input. Assert whether the original changed, the exact tie order, and the behavior for invalid rows rather than letting a runtime exception define the API.<\/p>\n<p>Python&#8217;s <a href=\"https:\/\/docs.python.org\/3\/howto\/sorting.html\">sorting HOWTO<\/a> explains key functions, stability, and operator helpers. The <a href=\"https:\/\/docs.python.org\/3\/library\/operator.html#operator.itemgetter\">itemgetter reference<\/a> documents positional extraction. Related guidance includes <a href=\"https:\/\/www.pythonpool.com\/python-lookup-table\/\">data-driven mappings<\/a> and <a href=\"https:\/\/www.pythonpool.com\/python-testing-framework\/\">sorting tests<\/a>.<\/p>\n<p>For related data-driven ordering, compare <a href=\"https:\/\/www.pythonpool.com\/python-lookup-table\/\">explicit mappings<\/a>, <a href=\"https:\/\/www.pythonpool.com\/python-dictionary-size\/\">collection operations<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/python-testing-framework\/\">sorting tests<\/a> when defining a stable key contract.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_sort_a_list_of_lists_by_the_first_item\"><\/span>How do I sort a list of lists by the first item?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use sorted(rows, key=lambda row: row[0]) or operator.itemgetter(0) when every inner list has the required position.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_sorted_and_listsort\"><\/span>What is the difference between sorted() and list.sort()?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>sorted() returns a new list, while list.sort() modifies the existing list in place and returns None.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_sort_by_multiple_values\"><\/span>How do I sort by multiple values?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Return a tuple from the key function, such as (row[0], row[1]), and use reverse or per-field transformations when the directions differ.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_sort_lists_of_different_lengths_safely\"><\/span>How do I sort lists of different lengths safely?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Validate the inner structure or use a key that handles missing positions explicitly; do not let an IndexError or mixed incomparable types define the behavior accidentally.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"How do I sort a list of lists by the first item?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use sorted(rows, key=lambda row: row[0]) or operator.itemgetter(0) when every inner list has the required position.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between sorted() and list.sort()?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"sorted() returns a new list, while list.sort() modifies the existing list in place and returns None.\"}},{\"@type\":\"Question\",\"name\":\"How do I sort by multiple values?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Return a tuple from the key function, such as (row[0], row[1]), and use reverse or per-field transformations when the directions differ.\"}},{\"@type\":\"Question\",\"name\":\"How do I sort lists of different lengths safely?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Validate the inner structure or use a key that handles missing positions explicitly; do not let an IndexError or mixed incomparable types define the behavior accidentally.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sort a Python list of lists by an index, length, sum, or multiple keys with sorted(), list.sort(), and operator.itemgetter while preserving clear mutation semantics.<\/p>\n","protected":false},"author":3,"featured_media":34014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[296,15],"tags":[2010,2025,2019,2021,2009,2012,2024,2016,2013,2018,2014,2011,2017,2023],"class_list":["post-3863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","category-tutorials","tag-how-to-sort-a-list-of-lists-in-python","tag-how-to-sort-list-of-lists-in-python","tag-python-sort-a-list-of-lists","tag-python-sort-a-list-of-lists-by-element","tag-python-sort-list-of-lists","tag-python-sort-list-of-lists-by-first-element","tag-python-sort-list-of-lists-by-multiple-keys","tag-python-sort-list-of-lists-by-second-element","tag-sort-a-list-of-lists-python","tag-sort-list-of-lists-by-element-python","tag-sort-list-of-lists-by-first-element-python","tag-sort-list-of-lists-by-second-element-python","tag-sort-list-of-lists-python","tag-sort-python-list-of-lists","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Sort List of Lists: key, itemgetter, and Multiple Keys<\/title>\n<meta name=\"description\" content=\"Sort a Python list of lists by an index, length, sum, or multiple keys with sorted(), list.sort(), and itemgetter.\" \/>\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\/python-sort-list-of-lists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Sort List of Lists: key, itemgetter, and Multiple Keys\" \/>\n<meta property=\"og:description\" content=\"Sort a Python list of lists by an index, length, sum, or multiple keys with sorted(), list.sort(), and operator.itemgetter while preserving clear mutation semantics.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-10T07:29:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T06:57:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-b092.png\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-b092.png\" \/>\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=\"Ashwini Mandani\" \/>\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:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"Python Sort List of Lists: key, itemgetter, and Multiple Keys\",\"datePublished\":\"2020-08-10T07:29:30+00:00\",\"dateModified\":\"2026-07-13T06:57:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/\"},\"wordCount\":1188,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-sort-list-of-lists-guide-pythonpool.png\",\"keywords\":[\"how to sort a list of lists in python\",\"how to sort list of lists in python\",\"python sort a list of lists\",\"python sort a list of lists by element\",\"python sort list of lists\",\"python sort list of lists by first element\",\"python sort list of lists by multiple keys\",\"python sort list of lists by second element\",\"sort a list of lists python\",\"sort list of lists by element python\",\"sort list of lists by first element python\",\"sort list of lists by second element python\",\"sort list of lists python\",\"sort python list of lists\"],\"articleSection\":[\"How to\",\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/\",\"name\":\"Python Sort List of Lists: key, itemgetter, and Multiple Keys\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-sort-list-of-lists-guide-pythonpool.png\",\"datePublished\":\"2020-08-10T07:29:30+00:00\",\"dateModified\":\"2026-07-13T06:57:50+00:00\",\"description\":\"Sort a Python list of lists by an index, length, sum, or multiple keys with sorted(), list.sort(), and itemgetter.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-sort-list-of-lists-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-sort-list-of-lists-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Python sort list of lists guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-sort-list-of-lists\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Sort List of Lists: key, itemgetter, and Multiple Keys\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"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\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/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\\\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Sort List of Lists: key, itemgetter, and Multiple Keys","description":"Sort a Python list of lists by an index, length, sum, or multiple keys with sorted(), list.sort(), and itemgetter.","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\/python-sort-list-of-lists\/","og_locale":"en_US","og_type":"article","og_title":"Python Sort List of Lists: key, itemgetter, and Multiple Keys","og_description":"Sort a Python list of lists by an index, length, sum, or multiple keys with sorted(), list.sort(), and operator.itemgetter while preserving clear mutation semantics.","og_url":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/","og_site_name":"Python Pool","article_published_time":"2020-08-10T07:29:30+00:00","article_modified_time":"2026-07-13T06:57:50+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-b092.png","type":"","width":"","height":""}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-b092.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"Python Sort List of Lists: key, itemgetter, and Multiple Keys","datePublished":"2020-08-10T07:29:30+00:00","dateModified":"2026-07-13T06:57:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/"},"wordCount":1188,"commentCount":4,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-guide-pythonpool.png","keywords":["how to sort a list of lists in python","how to sort list of lists in python","python sort a list of lists","python sort a list of lists by element","python sort list of lists","python sort list of lists by first element","python sort list of lists by multiple keys","python sort list of lists by second element","sort a list of lists python","sort list of lists by element python","sort list of lists by first element python","sort list of lists by second element python","sort list of lists python","sort python list of lists"],"articleSection":["How to","Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/","url":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/","name":"Python Sort List of Lists: key, itemgetter, and Multiple Keys","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-guide-pythonpool.png","datePublished":"2020-08-10T07:29:30+00:00","dateModified":"2026-07-13T06:57:50+00:00","description":"Sort a Python list of lists by an index, length, sum, or multiple keys with sorted(), list.sort(), and itemgetter.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-sort-list-of-lists-guide-pythonpool.png","width":1350,"height":650,"caption":"Python sort list of lists guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-sort-list-of-lists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python Sort List of Lists: key, itemgetter, and Multiple Keys"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","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":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/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\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3863","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3863"}],"version-history":[{"count":16,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3863\/revisions"}],"predecessor-version":[{"id":41409,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3863\/revisions\/41409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/34014"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}