{"id":10459,"date":"2025-08-19T12:24:20","date_gmt":"2025-08-19T12:24:20","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=10459"},"modified":"2025-08-19T12:24:20","modified_gmt":"2025-08-19T12:24:20","slug":"python-sort-lists-guide","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/python-sort-lists-guide\/","title":{"rendered":"Python Sort Lists \u2013 The Ultimate Guide to Sorting in Python"},"content":{"rendered":"<p>Sorting is something that most programmers do almost every day (could be every day depending on what you do. Sorting is part of any programming you do such as sorting exam scores, sorting products by price, cleaning data, etc. Sorting is ubiquitous.<\/p>\n<p>Python Sort Lists is super simple and really powerful. You don&#8217;t need to create anything because Python gives you tools that can do sorting for simple lists of numbers all the way to sorting with lots of custom logic with sort() and sorted().<\/p>\n<figure id=\"attachment_10470\" aria-describedby=\"caption-attachment-10470\" style=\"width: 394px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-10470\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics.webp\" alt=\"python sort lists\" width=\"394\" height=\"344\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics.webp 595w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-300x262.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-380x331.webp 380w\" sizes=\"(max-width: 394px) 100vw, 394px\" \/><figcaption id=\"caption-attachment-10470\" class=\"wp-caption-text\">Python Sort Lists<\/figcaption><\/figure>\n<p>In this guide we will cover everything you need to know about python sort list: the difference between sort() and sorted(), sorting numbers, strings, tuples, dictionaries, custom sorting and a lambda function. By the end of this guide you will be able to tackle whatever python sorting list techniques you need.<\/p>\n<h2>\ud83d\udd11 Python Sort Lists Basics: sort() vs sorted()<\/h2>\n<figure id=\"attachment_10472\" aria-describedby=\"caption-attachment-10472\" style=\"width: 611px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-10472\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted.webp\" alt=\"sorting list python\" width=\"611\" height=\"407\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted.webp 1536w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted-300x200.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted-1024x683.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted-768x512.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted-380x253.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted-800x533.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Python-Sort-Lists-Basics-sort-vs-sorted-1160x773.webp 1160w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><figcaption id=\"caption-attachment-10472\" class=\"wp-caption-text\">Python Sort Lists Basics: sort() vs sorted()<\/figcaption><\/figure>\n<p data-start=\"2669\" data-end=\"2738\">When it comes to <strong>sorting a list in Python<\/strong> you have two options:<\/p>\n<ol>\n<li><strong>sort()<\/strong> \u2013 This method sorts the list in place, meaning it modifies the original list and does not return a copy.<\/li>\n<li><strong>sorted(list)<\/strong> &#8211; This returns a new sorted list and maintains the original list.<\/li>\n<\/ol>\n<p>\ud83d\udc49 Example:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">numbers = [5, 2, 9, 1, 7]\u00a0<br\/><br\/># Using sort()<br\/><br\/>numbers.sort()<br\/><br\/>print(&quot;Using sort():&quot;, numbers)\u00a0 # [1, 2, 5, 7, 9]<br\/><br\/>\u00a0# Using sorted()numbers = [5, 2, 9, 1, 7]<br\/><br\/>new_list = sorted(numbers)<br\/><br\/>print(&quot;Using sorted():&quot;, new_list)\u00a0 # [1, 2, 5, 7, 9]<br\/><br\/>print(&quot;Original list:&quot;, numbers)\u00a0\u00a0\u00a0 # [5, 2, 9, 1, 7]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p data-start=\"3278\" data-end=\"3408\">\ud83d\udc49 Use <strong>sort()<\/strong> when you don\u2019t need the original order.<br data-start=\"3337\" data-end=\"3340\" \/>\ud83d\udc49 Use <strong>sorted()<\/strong> when you want to preserve the original list.<\/p>\n<h2>\ud83d\udd22 Sorting Numbers in Python<\/h2>\n<p data-start=\"3449\" data-end=\"3534\">If you\u2019re working with integers or floats, <strong data-start=\"3492\" data-end=\"3512\">list python sort<\/strong> is straightforward.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">nums = [10, 3, 8, 2, 7]<br\/><br\/>nums.sort()<br\/><br\/>print(nums)\u00a0 # [2, 3, 7, 8, 10]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p data-start=\"3619\" data-end=\"3678\">To sort <strong data-start=\"3627\" data-end=\"3650\">in descending order<\/strong>, just add reverse=True:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">nums = [10, 3, 8, 2, 7]<br\/><br\/>nums.sort(reverse=True)<br\/><br\/>print(nums)\u00a0 # [10, 8, 7, 3, 2]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2>\ud83d\udd24 Sorting Strings in Python<\/h2>\n<p data-start=\"3814\" data-end=\"3873\">Sorting strings works alphabetically (lexicographically).<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">words = [&quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;, &quot;date&quot;]<br\/><br\/>words.sort()<br\/><br\/>print(words)\u00a0 # [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;, &#039;date&#039;]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p data-start=\"4003\" data-end=\"4032\">Sorting in reverse (Z \u2192 A):<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">words.sort(reverse=True)<br\/><br\/>print(words)\u00a0 # [&#039;date&#039;, &#039;cherry&#039;, &#039;banana&#039;, &#039;apple&#039;]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2>\ud83d\udee0 Sorting with Key Parameter<\/h2>\n<p data-start=\"4168\" data-end=\"4297\">The real power of <strong data-start=\"4186\" data-end=\"4209\">sorting list python<\/strong> comes with the key parameter. It lets you define <strong data-start=\"4261\" data-end=\"4268\">how<\/strong> the list should be sorted.<\/p>\n<h4 data-start=\"4299\" data-end=\"4338\">Example: Sort by Length of Words<\/h4>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">words = [&quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;, &quot;date&quot;]<br\/><br\/>words.sort(key=len)<br\/><br\/>print(words)\u00a0 # [&#039;date&#039;, &#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h4 data-start=\"4475\" data-end=\"4515\">Example: Case-Insensitive Sorting<\/h4>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">words = [&quot;Banana&quot;, &quot;apple&quot;, &quot;Cherry&quot;, &quot;date&quot;]<br\/><br\/>words.sort(key=str.lower)<br\/><br\/>print(words)\u00a0 # [&#039;apple&#039;, &#039;Banana&#039;, &#039;Cherry&#039;, &#039;date&#039;]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2>\ud83e\uddd1\u200d\ud83d\udcbb Sorting with Lambda Functions<\/h2>\n<p data-start=\"4704\" data-end=\"4766\">Sometimes you need custom logic. Enter <strong data-start=\"4743\" data-end=\"4763\">lambda functions<\/strong>.<\/p>\n<h4 data-start=\"4768\" data-end=\"4806\">Example: Sort by Last Character<\/h4>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">words = [&quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;, &quot;date&quot;]<br\/><br\/>words.sort(key=lambda x: x[-1])<br\/><br\/>print(words)\u00a0 # [&#039;banana&#039;, &#039;apple&#039;, &#039;date&#039;, &#039;cherry&#039;]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2>\ud83d\udcc2 Sorting a List of Tuples<\/h2>\n<p data-start=\"4993\" data-end=\"5048\">Lists of tuples are common in Python (like datasets).<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">students = [(&quot;Alice&quot;, 85), (&quot;Bob&quot;, 92), (&quot;Charlie&quot;, 78)]\u00a0<br\/><br\/># Sort by marksstudents.sort(key=lambda x: x[1])<br\/><br\/>print(students)\u00a0 # [(&#039;Charlie&#039;, 78), (&#039;Alice&#039;, 85), (&#039;Bob&#039;, 92)]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2>\ud83d\udcd1 Sorting a List of Dictionaries<\/h2>\n<p data-start=\"5282\" data-end=\"5349\">If you\u2019ve ever handled JSON data, you\u2019ll know how useful this is.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">people = [\u00a0\u00a0\u00a0 {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25},\u00a0\u00a0\u00a0 {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 20},\u00a0\u00a0\u00a0 {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 30}]\u00a0<br\/><br\/># Sort by age<br\/><br\/>sorted_people = sorted(people, key=lambda x: x[&quot;age&quot;])<br\/><br\/>print(sorted_people)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p data-start=\"5571\" data-end=\"5580\">Output:<\/p>\n<p><span data-state=\"closed\">[{<\/span>&#8216;name&#8217;: &#8216;Bob&#8217;, &#8216;age&#8217;: 20}, {&#8216;name&#8217;: &#8216;Alice&#8217;, &#8216;age&#8217;: 25}, {&#8216;name&#8217;: &#8216;Charlie&#8217;, &#8216;age&#8217;: 30}]<\/p>\n<h2>\u26a1 Performance of Python List Sorting<\/h2>\n<p data-start=\"5734\" data-end=\"5789\">Python uses <strong data-start=\"5746\" data-end=\"5757\">Time sort<\/strong> for sorting lists, which has:<\/p>\n<ul>\n<li data-start=\"5792\" data-end=\"5848\"><strong data-start=\"5792\" data-end=\"5806\">O(n log n)<\/strong> average and worst-case time complexity.<\/li>\n<li data-start=\"5851\" data-end=\"5932\">Optimized for real-world data, making it one of the fastest sorting algorithms.<\/li>\n<\/ul>\n<h2>\ud83c\udf0d Real-World Examples of Sorting in Python<\/h2>\n<figure id=\"attachment_10474\" aria-describedby=\"caption-attachment-10474\" style=\"width: 599px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-10474\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python.webp\" alt=\"python sort lists\" width=\"599\" height=\"399\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python.webp 1536w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python-300x200.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python-1024x683.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python-768x512.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python-380x253.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python-800x533.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Examples-of-Sorting-in-Python-1160x773.webp 1160w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><figcaption id=\"caption-attachment-10474\" class=\"wp-caption-text\">Real-World Examples of Sorting in Python<\/figcaption><\/figure>\n<p data-start=\"5990\" data-end=\"6054\">Applying sorting algorithms:<\/p>\n<ul>\n<li><strong>E-Commerce<\/strong>: Sort a product&#8217;s price, rating, or popularity.<\/li>\n<li><strong>Education<\/strong>: Sort students by marks or GPA.<\/li>\n<li><strong>Data Science<\/strong>: Sort data-sets before processing.<\/li>\n<li><strong>Games<\/strong>: Sort a leader-board by score.<\/li>\n<\/ul>\n<p>\ud83d\udc49 Example: Sorting Student Grades<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">grades = [88, 92, 74, 63, 95, 82]<br\/><br\/>grades.sort(reverse=True)<br\/><br\/>print(&quot;Top scores:&quot;, grades[:3])\u00a0 # [95, 92, 88]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2>\u2694\ufe0f Python sort() vs sorted(): Which One to Use?<\/h2>\n<p>Python&#8217;s sorting algorithm is Time sort and it has:\u00b7<\/p>\n<ul>\n<li>O(n log n) average and worst-case time complexity.\u00b7<\/li>\n<li>Optimized performance on real-world data resulting in being one of the fastest sorting algorithms.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">\u00a0tuple_data = (3, 1, 4)<br\/><br\/>print(sorted(tuple_data))\u00a0 # [1, 3, 4]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2>FAQs on Python Sort Lists<\/h2>\n<p data-start=\"6717\" data-end=\"6832\"><strong data-start=\"6717\" data-end=\"6775\">Q1: Can you sort a list in Python without changing it?<\/strong><br data-start=\"6775\" data-end=\"6778\" \/>Yes, use sorted(list) instead of list.sort().<\/p>\n<p data-start=\"6834\" data-end=\"6956\"><strong data-start=\"6834\" data-end=\"6886\">Q2: How to sort list python in descending order?<\/strong><br data-start=\"6886\" data-end=\"6889\" \/>Use list.sort(reverse=True) or sorted(list, reverse=True).<\/p>\n<p data-start=\"6958\" data-end=\"7087\"><strong data-start=\"6958\" data-end=\"7022\">Q3: What\u2019s the difference between sort and sorted in Python?<\/strong><br data-start=\"7022\" data-end=\"7025\" \/>sort() modifies the list, sorted() returns a new one.<\/p>\n<p data-start=\"7089\" data-end=\"7203\"><strong data-start=\"7089\" data-end=\"7134\">Q4: Can I sort complex objects in Python?<\/strong><br data-start=\"7134\" data-end=\"7137\" \/>Yes, use the key parameter with lambda or custom functions.<\/p>\n<h2>Conclusion<\/h2>\n<p data-start=\"7229\" data-end=\"7577\"><strong>Sorting<\/strong> is one of the most important skills in Python programming, whether you\u2019re using numbers, strings, tuples, or dictionaries, Python has all the functionality, from <strong>sort()<\/strong> to <strong>sorted()<\/strong>. Once you understand how to wield the key parameter skillfully, paired with lambda functions, you\u2019re able to <strong>sort lists in Python<\/strong> in virtually any way you can think of.<\/p>\n<p>The next time you\u2019re faced with data or project work, keep these techniques in mind to keep your <strong>sorting lists python<\/strong> tasks efficient and clean.<\/p>\n<h2>Related Reads<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/python-list-sorting-guide\/\">How to Sort a List in Python: A Complete Guide with Examples<\/a><\/li>\n<li><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/insertion-sort-time-complexity-guide\/\">Insertion Sort Time Complexity: Complete Guide for Beginners &amp; Pros<\/a><\/li>\n<li><a href=\"https:\/\/www.wikitechy.com\/how-to-use-lists-in-python\/\" target=\"_blank\" rel=\"noopener\">How to Use Lists in Python &#8211; A Complete Guide Learn in 2025<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Sorting is something that most programmers do almost every day (could be every day depending on what you do. Sorting is part of any programming you do such as sorting exam scores, sorting products by price, cleaning data, etc. Sorting is ubiquitous. Python Sort Lists is super simple and really powerful. You don&#8217;t need to [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":10469,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3236],"tags":[8454,8462,8452,8456,8457,8460,8458,8459,8455,8461,8453],"class_list":["post-10459","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-list-python-sort","tag-python-list-sorting-methods","tag-python-sort-list","tag-python-sort-list-ascending","tag-python-sort-list-descending","tag-python-sort-list-of-dictionaries","tag-python-sort-list-of-strings","tag-python-sort-list-of-tuples","tag-python-sort-vs-sorted","tag-python-sorted-function","tag-sorting-list-python"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=10459"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10459\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/10469"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=10459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=10459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=10459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}