{"id":3247,"date":"2026-11-25T11:43:04","date_gmt":"2026-11-25T11:43:04","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3247"},"modified":"2026-11-25T11:43:46","modified_gmt":"2026-11-25T11:43:46","slug":"python-list-sort","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-list-sort\/","title":{"rendered":"Python List sort() Method with Examples (reverse, key, sorted vs sort)"},"content":{"rendered":"<p>The <code>sort()<\/code> method arranges the items of a Python list in order, from smallest to largest by default. It changes the list <strong>in place<\/strong> and returns <code>None<\/code>. With its two optional arguments, <code>reverse<\/code> and <code>key<\/code>, you can sort in descending order, ignore capital letters, sort by length, or sort records by any field you choose.<\/p>\n<h2>Syntax<\/h2>\n<pre class=\"wp-block-code\"><code>list.sort(key=None, reverse=False)<\/code><\/pre>\n<ul>\n<li><code>reverse<\/code>: set to <code>True<\/code> for descending order.<\/li>\n<li><code>key<\/code>: a function that tells Python <em>what<\/em> to compare for each item.<\/li>\n<\/ul>\n<h2>Sort in Ascending Order<\/h2>\n<pre class=\"wp-block-code\"><code>nums = [29, 4, 42, 5, 31, 22, 39]\nnums.sort()\nprint(nums)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[4, 5, 22, 29, 31, 39, 42]<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-sort-METHOD.jpg\" alt=\"Python list sort method example\" \/><\/figure>\n<h2>Sort in Descending Order<\/h2>\n<pre class=\"wp-block-code\"><code>nums = [29, 4, 42, 5, 31, 22, 39]\nnums.sort(reverse=True)\nprint(nums)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[42, 39, 31, 29, 22, 5, 4]<\/code><\/pre>\n<h2>Sorting Strings (and the Capital-Letter Surprise)<\/h2>\n<pre class=\"wp-block-code\"><code>words = ['banana', 'apple', 'Cherry', 'date']\nwords.sort()\nprint(words)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['Cherry', 'apple', 'banana', 'date']<\/code><\/pre>\n<p>&#8220;Cherry&#8221; comes first because Python compares characters by their Unicode values, and all capital letters come before small letters. To sort alphabetically regardless of case, use <code>key=str.lower<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>words.sort(key=str.lower)\nprint(words)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['apple', 'banana', 'Cherry', 'date']<\/code><\/pre>\n<h2>Sort by Length with key=len<\/h2>\n<pre class=\"wp-block-code\"><code>words = ['apple', 'banana', 'Cherry', 'date']\nwords.sort(key=len)\nprint(words)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['date', 'apple', 'banana', 'Cherry']<\/code><\/pre>\n<p>&#8220;banana&#8221; and &#8220;Cherry&#8221; both have 6 letters, so they keep their original order. Python&#8217;s sort is <strong>stable<\/strong>: items that compare equal stay in the order they were in.<\/p>\n<h2>Sort a List of Tuples or Records<\/h2>\n<p>To sort students by marks, highest first, use a <code>lambda<\/code> as the key:<\/p>\n<pre class=\"wp-block-code\"><code>students = [('Riya', 88), ('Aman', 95), ('Kabir', 88), ('Zoya', 72)]\nstudents.sort(key=lambda s: s[1], reverse=True)\nprint(students)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[('Aman', 95), ('Riya', 88), ('Kabir', 88), ('Zoya', 72)]<\/code><\/pre>\n<p>Riya stays ahead of Kabir because both have 88 and Riya came first in the original list.<\/p>\n<h2>sort() vs sorted()<\/h2>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th><code>list.sort()<\/code><\/th>\n<th><code>sorted(iterable)<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Changes the original?<\/td>\n<td>Yes, in place<\/td>\n<td>No, returns a new list<\/td>\n<\/tr>\n<tr>\n<td>Returns<\/td>\n<td><code>None<\/code><\/td>\n<td>A new sorted list<\/td>\n<\/tr>\n<tr>\n<td>Works on<\/td>\n<td>Lists only<\/td>\n<td>Any iterable: list, tuple, string, set, dictionary keys<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"wp-block-code\"><code>y = [3, 1, 2]\nz = sorted(y)\nprint(y, z)              # [3, 1, 2] [1, 2, 3]\n\nprint(sorted((5, 2, 9))) # [2, 5, 9]<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-sort-METHOD-1.jpg\" alt=\"Python list sort reverse example\" \/><\/figure>\n<h2>Common Mistakes<\/h2>\n<h3>1. Saving the result of sort()<\/h3>\n<pre class=\"wp-block-code\"><code>x = [3, 1, 2]\nresult = x.sort()\nprint(result, x)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>None [1, 2, 3]<\/code><\/pre>\n<p><code>sort()<\/code> returns <code>None<\/code>. Use the list itself after sorting, or use <code>sorted()<\/code> if you need a new list.<\/p>\n<h3>2. Sorting mixed types<\/h3>\n<pre class=\"wp-block-code\"><code>[3, 'a', 1].sort()<\/code><\/pre>\n<pre class=\"wp-block-code\"><code>TypeError: '&lt;' not supported between instances of 'str' and 'int'<\/code><\/pre>\n<p>Python 3 cannot compare numbers with strings. Convert the items to one type first, or pass a <code>key<\/code> such as <code>key=str<\/code>.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>Does sort() return anything?<\/h3>\n<p>No, it returns <code>None<\/code> and sorts the list in place.<\/p>\n<h3>How do I sort a list in descending order in Python?<\/h3>\n<p><code>my_list.sort(reverse=True)<\/code>, or <code>sorted(my_list, reverse=True)<\/code> for a new list.<\/p>\n<h3>Is Python&#8217;s sort stable?<\/h3>\n<p>Yes. Items with equal keys keep their original relative order.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/python-structural-pattern-matching\/\">Python structural pattern matching<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The sort() method arranges the items of a Python list in order, from smallest to largest by default. It changes the list in place and returns None. With its two optional arguments, reverse and key, you can sort in descending order, ignore capital letters, sort by length, or sort records by any field you choose. &#8230; <a title=\"Python List sort() Method with Examples (reverse, key, sorted vs sort)\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-list-sort\/\" aria-label=\"Read more about Python List sort() Method with Examples (reverse, key, sorted vs sort)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3247","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3247"}],"version-history":[{"count":4,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3247\/revisions"}],"predecessor-version":[{"id":6159,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3247\/revisions\/6159"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}