{"id":4652,"date":"2025-03-06T07:47:50","date_gmt":"2025-03-06T07:47:50","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=4652"},"modified":"2025-07-22T10:20:09","modified_gmt":"2025-07-22T10:20:09","slug":"python-list-sorting-guide","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/python-list-sorting-guide\/","title":{"rendered":"How to Sort a List in Python: A Complete Guide with Examples"},"content":{"rendered":"<h2 data-start=\"111\" data-end=\"169\"><strong data-start=\"111\" data-end=\"167\">Introduction: How to Python Sort List Easily in 2025<\/strong><\/h2>\n<p><strong data-start=\"165\" data-end=\"254\">Sorting a list in Python is easy with the built-in <code class=\"\" data-line=\"\">sort()<\/code> and <code class=\"\" data-line=\"\">sorted()<\/code> functions.<\/strong> If you&#8217;re searching for how to <strong data-start=\"286\" data-end=\"306\">python sort list<\/strong>\u2014whether it&#8217;s numbers, strings, or custom objects\u2014this beginner-friendly guide has you covered. We\u2019ll walk through 10 powerful methods to sort lists in Python, complete with real-world examples to boost your data skills in 2025.<\/p>\n<h2><strong>\ud83d\udd39 Key Highlights<\/strong><\/h2>\n<p>\u2705 Explore 10 different techniques for <strong>sorting a list in Python<\/strong><br \/>\n\u2705 Learn the syntax of the <code class=\"\" data-line=\"\">sort()<\/code> method &amp; <code class=\"\" data-line=\"\">sorted()<\/code> function<br \/>\n\u2705 Discover how to sort in <strong>ascending and descending order<\/strong><br \/>\n\u2705 Use <strong>custom key functions<\/strong> for advanced sorting<br \/>\n\u2705 Learn how to <strong>sort without modifying the original list<\/strong><br \/>\n\u2705 Hands-on <strong>Python examples<\/strong> to solidify your understanding<\/p>\n<hr \/>\n<h2><strong>\ud83d\udccc Why is it Important to learn Sort List Python ?<\/strong><\/h2>\n<p>Sorting is a fundamental operation when working with data. Whether you&#8217;re <strong>analyzing datasets, searching values, ranking elements, or improving performance<\/strong>, sorting helps organize information efficiently.<\/p>\n<p>Python simplifies sorting using the <strong><code class=\"\" data-line=\"\">sort()<\/code> method<\/strong> (which modifies the list in place) and the <strong><code class=\"\" data-line=\"\">sorted()<\/code> function<\/strong> (which returns a new sorted list). Let\u2019s dive into various ways you can <strong>sort a list in <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-tutorial\" target=\"_blank\" rel=\"noopener\">Python<\/a><\/strong> effectively! \ud83d\udc0d<\/p>\n<hr \/>\n<h2><strong>\ud83d\udd39 1. Sorting a List in Python Using <code class=\"\" data-line=\"\">sort()<\/code> Method<\/strong><\/h2>\n<p>The <strong><code class=\"\" data-line=\"\">sort()<\/code> method<\/strong> modifies a list directly and sorts it <strong>in ascending order<\/strong> by default.<\/p>\n<h3><strong>\ud83d\udcdd Syntax:<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">list_name.sort(reverse=False)\n<\/code><\/pre>\n<ul>\n<li>By default, sorting is <strong>ascending<\/strong> (<code class=\"\" data-line=\"\">reverse=False<\/code>).<\/li>\n<li>Set <code class=\"\" data-line=\"\">reverse=True<\/code> to sort in <strong>descending order<\/strong>.<\/li>\n<\/ul>\n<h3><strong>\u2705 Example: Sorting in Ascending Order<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">numbers = [4, 2, 1, 3, 5]\nnumbers.sort()\nprint(numbers)  # Output: [1, 2, 3, 4, 5]\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 2. Sorting a List in Python in Descending Order<\/strong><\/h2>\n<p>To <strong>sort a list in descending order<\/strong>, use <code class=\"\" data-line=\"\">reverse=True<\/code>.<\/p>\n<figure id=\"attachment_4653\" aria-describedby=\"caption-attachment-4653\" style=\"width: 1280px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-4653 size-full\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/maxresdefault-9.jpg\" alt=\"python sort listpython sorted list\nsort list python\" width=\"1280\" height=\"720\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/maxresdefault-9.jpg 1280w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/maxresdefault-9-300x169.jpg 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/maxresdefault-9-1024x576.jpg 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/maxresdefault-9-768x432.jpg 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><figcaption id=\"caption-attachment-4653\" class=\"wp-caption-text\">Sorting list in Python<\/figcaption><\/figure>\n<pre><code class=\"language-python\" data-line=\"\">numbers = [4, 2, 1, 3, 5]\nnumbers.sort(reverse=True)\nprint(numbers)  # Output: [5, 4, 3, 2, 1]\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 3. Sorting a List in Python Using a Custom Key<\/strong><\/h2>\n<p>Python allows <strong>custom sorting<\/strong> based on a function using the <code class=\"\" data-line=\"\">key<\/code> parameter.<\/p>\n<h3><strong>\u2705 Example: Sorting by String Length<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">fruits = [&quot;banana&quot;, &quot;apple&quot;, &quot;orange&quot;, &quot;kiwi&quot;, &quot;mango&quot;]\nfruits.sort(key=len)\nprint(fruits)  # Output: [&quot;kiwi&quot;, &quot;mango&quot;, &quot;apple&quot;, &quot;banana&quot;, &quot;orange&quot;]\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 4. Sorting Without Modifying the Original List<\/strong><\/h2>\n<p>The <strong><code class=\"\" data-line=\"\">sorted()<\/code> function<\/strong> returns a new sorted list while keeping the original list unchanged.<\/p>\n<pre><code class=\"language-python\" data-line=\"\">numbers = [4, 2, 1, 3, 5]\nsorted_numbers = sorted(numbers)\nprint(sorted_numbers)  # Output: [1, 2, 3, 4, 5]\nprint(numbers)  # Output: [4, 2, 1, 3, 5]  # Original list remains unchanged\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 5. Sorting a List of Tuples in Python<\/strong><\/h2>\n<p>Sort a <strong>list of tuples<\/strong> based on a specific element using the <code class=\"\" data-line=\"\">key<\/code> parameter.<\/p>\n<figure id=\"attachment_4654\" aria-describedby=\"caption-attachment-4654\" style=\"width: 1920px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-4654 size-full\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Your-paragraph-text-56-.jpg\" alt=\"python sort listpython sorted list\nsort list python\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Your-paragraph-text-56-.jpg 1920w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Your-paragraph-text-56--300x169.jpg 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Your-paragraph-text-56--1024x576.jpg 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Your-paragraph-text-56--768x432.jpg 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Your-paragraph-text-56--1536x864.jpg 1536w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/><figcaption id=\"caption-attachment-4654\" class=\"wp-caption-text\">python sort list<\/figcaption><\/figure>\n<pre><code class=\"language-python\" data-line=\"\">students = [(&quot;John&quot;, 90), (&quot;Alice&quot;, 85), (&quot;Bob&quot;, 95)]\nstudents.sort(key=lambda x: x[1])\nprint(students)  # Output: [(&quot;Alice&quot;, 85), (&quot;John&quot;, 90), (&quot;Bob&quot;, 95)]\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 6. Sorting a List of Dictionaries in Python<\/strong><\/h2>\n<p>Sort a <strong>list of dictionaries<\/strong> by a specific key.<\/p>\n<pre><code class=\"language-python\" data-line=\"\">students = [{&quot;name&quot;: &quot;John&quot;, &quot;score&quot;: 90}, {&quot;name&quot;: &quot;Alice&quot;, &quot;score&quot;: 85}, {&quot;name&quot;: &quot;Bob&quot;, &quot;score&quot;: 95}]\nstudents.sort(key=lambda x: x[&quot;score&quot;])\nprint(students)\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 7. Case-Insensitive Sorting in Python<\/strong><\/h2>\n<p>To <strong>sort strings without case sensitivity<\/strong>, use <code class=\"\" data-line=\"\">str.lower<\/code> as the key.<\/p>\n<pre><code class=\"language-python\" data-line=\"\">words = [&quot;banana&quot;, &quot;Apple&quot;, &quot;orange&quot;]\nwords.sort(key=str.lower)\nprint(words)  # Output: [&quot;Apple&quot;, &quot;banana&quot;, &quot;orange&quot;]\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 8. Sorting a List in Python Using Reverse Sorting<\/strong><\/h2>\n<p>Sort lists in <strong>descending order<\/strong> using the <code class=\"\" data-line=\"\">reverse<\/code> parameter.<\/p>\n<figure id=\"attachment_4656\" aria-describedby=\"caption-attachment-4656\" style=\"width: 1920px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-4656 size-full\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Reverse-a-String-in-Python_Water.png\" alt=\"python sort listpython sorted list\nsort list python\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Reverse-a-String-in-Python_Water.png 1920w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Reverse-a-String-in-Python_Water-300x169.png 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Reverse-a-String-in-Python_Water-1024x576.png 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Reverse-a-String-in-Python_Water-768x432.png 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Reverse-a-String-in-Python_Water-1536x864.png 1536w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/><figcaption id=\"caption-attachment-4656\" class=\"wp-caption-text\">Sorting a list in Python<\/figcaption><\/figure>\n<pre><code class=\"language-python\" data-line=\"\">numbers = [10, 3, 7, 1, 9]\nprint(sorted(numbers, reverse=True))  # Output: [10, 9, 7, 3, 1]\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 9. Sorting Using <code class=\"\" data-line=\"\">itemgetter<\/code> from <code class=\"\" data-line=\"\">operator<\/code> Module<\/strong><\/h2>\n<p>A more efficient way to sort lists of tuples or dictionaries.<\/p>\n<pre><code class=\"language-python\" data-line=\"\">from operator import itemgetter\nstudents = [(&quot;John&quot;, 90), (&quot;Alice&quot;, 85), (&quot;Bob&quot;, 95)]\nstudents.sort(key=itemgetter(1))\nprint(students)\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83d\udd39 10. Sorting Using <code class=\"\" data-line=\"\">functools.cmp_to_key<\/code><\/strong><\/h2>\n<p>For advanced sorting scenarios, <code class=\"\" data-line=\"\">cmp_to_key<\/code> allows <strong>custom comparison functions<\/strong>.<\/p>\n<pre><code class=\"language-python\" data-line=\"\">from functools import cmp_to_key\ndef compare(x, y):\n    return x - y\nnumbers = [5, 2, 8, 1, 9]\nnumbers.sort(key=cmp_to_key(compare))\nprint(numbers)  # Output: [1, 2, 5, 8, 9]\n<\/code><\/pre>\n<hr \/>\n<h2><strong>\ud83c\udfaf Conclusion: Master the Python Sorted List Techniques \ud83d\ude80<\/strong><\/h2>\n<p>Sorting is a fundamental operation that enhances <strong>data organization and efficiency<\/strong>. With Python\u2019s built-in <code class=\"\" data-line=\"\">sort()<\/code> and <code class=\"\" data-line=\"\">sorted()<\/code> functions, <strong>sort list <a href=\"https:\/\/www.kaashivinfotech.com\/python-full-stack-development-course-in-chennai\/\">Python<\/a><\/strong> becomes easy and flexible.<\/p>\n<h3><strong>\u2728 Key Takeaways:<\/strong><\/h3>\n<p>\u2705 Use <code class=\"\" data-line=\"\">sort()<\/code> for in-place sorting and <code class=\"\" data-line=\"\">sorted()<\/code> for a new sorted list.<br \/>\n\u2705 Apply <code class=\"\" data-line=\"\">key<\/code> parameter for custom sorting logic.<br \/>\n\u2705 Sort lists of <strong>strings, numbers, tuples, and dictionaries<\/strong> effortlessly.<br \/>\n\u2705 Leverage advanced techniques like <strong><code class=\"\" data-line=\"\">itemgetter<\/code> and <code class=\"\" data-line=\"\">cmp_to_key<\/code><\/strong> for optimal performance.<\/p>\n<p>\ud83d\udd17 <strong>Further Reading:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html\" target=\"_blank\" rel=\"noopener\">Python Official Documentation<\/a><\/li>\n<li><a href=\"https:\/\/www.wikitechy.com\/python-list-sort\/\" target=\"_blank\" rel=\"noopener\">Sorting in Python\u00a0<\/a><\/li>\n<\/ul>\n<p>Now that you&#8217;ve mastered <strong>sorting a list in Python<\/strong>, try experimenting with different datasets and improve your coding skills! \ud83d\ude80\ud83d\udc0d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: How to Python Sort List Easily in 2025 Sorting a list in Python is easy with the built-in sort() and sorted() functions. If you&#8217;re searching for how to python sort list\u2014whether it&#8217;s numbers, strings, or custom objects\u2014this beginner-friendly guide has you covered. We\u2019ll walk through 10 powerful methods to sort lists in Python, complete [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":4660,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3236,3203],"tags":[3248,3253,3249,3255,3254,3252,3250,3251],"class_list":["post-4652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-programming","tag-how-to-sort-a-list-in-python-without-sort-function","tag-sort-function-in-python","tag-sort-list-of-numbers-python","tag-sort-string-python","tag-sort-vs-sorted-python","tag-sorted-python","tag-sorting-a-list-in-python-example","tag-sorting-a-list-in-python-w3schools"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/4652","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=4652"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/4652\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/4660"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=4652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=4652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=4652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}