{"id":288,"date":"2020-10-06T03:39:37","date_gmt":"2020-10-06T03:39:37","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=288"},"modified":"2025-03-26T15:13:57","modified_gmt":"2025-03-26T15:13:57","slug":"python-sort-list","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-sort-list\/","title":{"rendered":"Python Sort List"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python List <code>sort()<\/code> method to sort a list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-list-sort-method'>Introduction to the Python List sort() method <a href=\"#introduction-to-the-python-list-sort-method\" class=\"anchor\" id=\"introduction-to-the-python-list-sort-method\" title=\"Anchor for Introduction to the Python List sort() method\">#<\/a><\/h2>\n\n\n\n<p>To sort a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a>, you use the <code>sort()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">list.sort()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>sort()<\/code> method sorts the original list in place. It means that the <code>sort()<\/code> method modifies the order of elements in the list. <\/p>\n\n\n\n<p>By default, the <code>sort()<\/code> method sorts the elements of a list using the less-than operator (<code>&lt;<\/code>).  In other words, it places the lower elements before the higher ones.<\/p>\n\n\n\n<p>To sort elements from higher to lower, you pass the <code>reverse=True<\/code> argument to the <code>sort()<\/code> method like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">list.sort(reverse=<span class=\"hljs-literal\">True<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sorting-a-list-of-strings'>Sorting a list of strings <a href=\"#sorting-a-list-of-strings\" class=\"anchor\" id=\"sorting-a-list-of-strings\" title=\"Anchor for Sorting a list of strings\">#<\/a><\/h2>\n\n\n\n<p>If a list contains strings, the <code>sort()<\/code> method sorts the string elements alphabetically. <\/p>\n\n\n\n<p>The following example uses the <code>sort()<\/code> method to sort the elements in the <code>guests<\/code> list alphabetically:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">guests = &#91;<span class=\"hljs-string\">'James'<\/span>, <span class=\"hljs-string\">'Mary'<\/span>, <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Patricia'<\/span>, <span class=\"hljs-string\">'Robert'<\/span>, <span class=\"hljs-string\">'Jennifer'<\/span>]\nguests.sort()\n\nprint(guests)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Z3Vlc3RzID0gWydKYW1lcycsICdNYXJ5JywgJ0pvaG4nLCAnUGF0cmljaWEnLCAnUm9iZXJ0JywgJ0plbm5pZmVyJ10KZ3Vlc3RzLnNvcnQoKQoKcHJpbnQoZ3Vlc3RzKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'James'<\/span>, <span class=\"hljs-string\">'Jennifer'<\/span>, <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Mary'<\/span>, <span class=\"hljs-string\">'Patricia'<\/span>, <span class=\"hljs-string\">'Robert'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the following example uses the <code>sort()<\/code> method with the <code>reverse=True<\/code> argument to sort the elements in the <code>guests<\/code> list in the reverse alphabetical order:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">guests = &#91;<span class=\"hljs-string\">'James'<\/span>, <span class=\"hljs-string\">'Mary'<\/span>, <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Patricia'<\/span>, <span class=\"hljs-string\">'Robert'<\/span>, <span class=\"hljs-string\">'Jennifer'<\/span>]\nguests.sort(reverse=<span class=\"hljs-literal\">True<\/span>)\n\nprint(guests)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Z3Vlc3RzID0gWydKYW1lcycsICdNYXJ5JywgJ0pvaG4nLCAnUGF0cmljaWEnLCAnUm9iZXJ0JywgJ0plbm5pZmVyJ10KZ3Vlc3RzLnNvcnQocmV2ZXJzZT1UcnVlKQoKcHJpbnQoZ3Vlc3RzKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'Robert'<\/span>, <span class=\"hljs-string\">'Patricia'<\/span>, <span class=\"hljs-string\">'Mary'<\/span>, <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Jennifer'<\/span>, <span class=\"hljs-string\">'James'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sorting-a-list-of-numbers'>Sorting a list of numbers <a href=\"#sorting-a-list-of-numbers\" class=\"anchor\" id=\"sorting-a-list-of-numbers\" title=\"Anchor for Sorting a list of numbers\">#<\/a><\/h2>\n\n\n\n<p>If a list contains numbers, the <code>sort()<\/code> method sorts the numbers from smallest to largest. <\/p>\n\n\n\n<p>The following example uses the <code>sort()<\/code> method to sort numbers in the <code>scores<\/code> list from smallest to largest:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">scores = &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">8<\/span>]\nscores.sort()\n\nprint(scores)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=c2NvcmVzID0gWzUsIDcsIDQsIDYsIDksIDhdCnNjb3Jlcy5zb3J0KCkKCnByaW50KHNjb3Jlcyk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To sort numbers from the largest to smallest, you use the <code>sort(reverse=True)<\/code> like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">scores = &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">8<\/span>]\nscores.sort(reverse=<span class=\"hljs-literal\">True<\/span>)\n\nprint(scores)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=c2NvcmVzID0gWzUsIDcsIDQsIDYsIDksIDhdCnNjb3Jlcy5zb3J0KHJldmVyc2U9VHJ1ZSkKCnByaW50KHNjb3Jlcyk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">4<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='sorting-a-list-of-tuples'>Sorting a list of tuples <a href=\"#sorting-a-list-of-tuples\" class=\"anchor\" id=\"sorting-a-list-of-tuples\" title=\"Anchor for Sorting a list of tuples\">#<\/a><\/h3>\n\n\n\n<p>Suppose that you have a list of  tuples like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">companies = &#91;(<span class=\"hljs-string\">'Google'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">134.81<\/span>),\n             (<span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">260.2<\/span>),\n             (<span class=\"hljs-string\">'Facebook'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">70.7<\/span>)]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you want to sort the companies list by revenue from highest to lowest. To do it:<\/p>\n\n\n\n<p>First, specify a sort key and pass it to the <code>sort()<\/code> method. To define a sort key, you create a function that accepts a tuple and returns the element that you want to sort by:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">sort_key<\/span><span class=\"hljs-params\">(company)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> company&#91;<span class=\"hljs-number\">2<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This <code>sort_key()<\/code> function accepts a tuple called <code>company<\/code> and returns the third element.<\/p>\n\n\n\n<p>Note that the company is a tuple e.g., <code>('Google', 2019, 134.81)<\/code>. And the <code>company[2]<\/code> references the revenue like <code>134.81<\/code> in this case.<\/p>\n\n\n\n<p>Second, pass the sort_key function to the <code>sort()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">companies.sort(key=sort_key, reverse=<span class=\"hljs-literal\">True<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>sort()<\/code> method will use the value returned by the <code>sort_key()<\/code> function for the comparisons. <\/p>\n\n\n\n<p>Note that you just pass the function name without the parentheses to the <code>sort()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">companies = &#91;(<span class=\"hljs-string\">'Google'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">134.81<\/span>),\n             (<span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">260.2<\/span>),\n             (<span class=\"hljs-string\">'Facebook'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">70.7<\/span>)]\n\n\n<span class=\"hljs-comment\"># define a sort key<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">sort_key<\/span><span class=\"hljs-params\">(company)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> company&#91;<span class=\"hljs-number\">2<\/span>]\n\n\n\n<span class=\"hljs-comment\"># sort the companies by revenue<\/span>\ncompanies.sort(key=sort_key, reverse=<span class=\"hljs-literal\">True<\/span>)\n\n<span class=\"hljs-comment\"># show the sorted companies<\/span>\nprint(companies)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29tcGFuaWVzID0gWygnR29vZ2xlJywgMjAxOSwgMTM0LjgxKSwKICAgICAgICAgICAgICgnQXBwbGUnLCAyMDE5LCAyNjAuMiksCiAgICAgICAgICAgICAoJ0ZhY2Vib29rJywgMjAxOSwgNzAuNyldCgoKIyBkZWZpbmUgYSBzb3J0IGtleQpkZWYgc29ydF9rZXkoY29tcGFueSk6CiAgICByZXR1cm4gY29tcGFueVsyXQoKCgojIHNvcnQgdGhlIGNvbXBhbmllcyBieSByZXZlbnVlCmNvbXBhbmllcy5zb3J0KGtleT1zb3J0X2tleSwgcmV2ZXJzZT1UcnVlKQoKIyBzaG93IHRoZSBzb3J0ZWQgY29tcGFuaWVzCnByaW50KGNvbXBhbmllcyk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;(<span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">260.2<\/span>), (<span class=\"hljs-string\">'Google'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">134.81<\/span>), (<span class=\"hljs-string\">'Facebook'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">70.7<\/span>)]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='using-lambda-expression'>Using lambda expression <a href=\"#using-lambda-expression\" class=\"anchor\" id=\"using-lambda-expression\" title=\"Anchor for Using lambda expression\">#<\/a><\/h3>\n\n\n\n<p>To make it more concise, Python allows you to define a function without a name with the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">lambda<\/span> arguments: expression<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>A function without a name is called an <strong>anonymous function<\/strong>. And this syntax is called a <strong><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/\">lambda expression<\/a><\/strong>. <\/p>\n\n\n\n<p>Technically, it&#8217;s equivalent to the following function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">name<\/span><span class=\"hljs-params\">(arguments)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> expression<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses the lambda expression to sort the companies by revenue from low to high:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">companies = &#91;(<span class=\"hljs-string\">'Google'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">134.81<\/span>),\n             (<span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">260.2<\/span>),\n             (<span class=\"hljs-string\">'Facebook'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">70.7<\/span>)]\n\n<span class=\"hljs-comment\"># sort the companies by revenue<\/span>\ncompanies.sort(key=<span class=\"hljs-keyword\">lambda<\/span> company: company&#91;<span class=\"hljs-number\">2<\/span>])\n\n<span class=\"hljs-comment\"># show the sorted companies<\/span>\nprint(companies)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29tcGFuaWVzID0gWygnR29vZ2xlJywgMjAxOSwgMTM0LjgxKSwKICAgICAgICAgICAgICgnQXBwbGUnLCAyMDE5LCAyNjAuMiksCiAgICAgICAgICAgICAoJ0ZhY2Vib29rJywgMjAxOSwgNzAuNyldCgojIHNvcnQgdGhlIGNvbXBhbmllcyBieSByZXZlbnVlCmNvbXBhbmllcy5zb3J0KGtleT1sYW1iZGEgY29tcGFueTogY29tcGFueVsyXSkKCiMgc2hvdyB0aGUgc29ydGVkIGNvbXBhbmllcwpwcmludChjb21wYW5pZXMp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;(<span class=\"hljs-string\">'Facebook'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">70.7<\/span>), (<span class=\"hljs-string\">'Google'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">134.81<\/span>), (<span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">2019<\/span>, <span class=\"hljs-number\">260.2<\/span>)]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Python List <code>sort()<\/code> method to sort a list in place.<\/li>\n\n\n\n<li>The <code>sort()<\/code> method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest.<\/li>\n\n\n\n<li>Use the <code>sort(reverse=True)<\/code> to reverse the default sort order.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=list-sort\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"288\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-sort-list\/\"\n\t\t\t\tdata-post-title=\"Python Sort List\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"288\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-sort-list\/\"\n\t\t\t\tdata-post-title=\"Python Sort List\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the Python List sort() method to sort a list. Introduction to the Python List sort() method # To sort a list, you use the sort() method: The sort() method sorts the original list in place. It means that the sort() method modifies the order of elements [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":27,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-288","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/288\/revisions"}],"predecessor-version":[{"id":7057,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/288\/revisions\/7057"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}