{"id":68,"date":"2024-01-25T09:33:04","date_gmt":"2024-01-25T09:33:04","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=68"},"modified":"2024-01-25T09:33:05","modified_gmt":"2024-01-25T09:33:05","slug":"python-sort-list","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-sort-list\/","title":{"rendered":"Python Sort List"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python List&nbsp;<code>sort()<\/code>&nbsp;method to sort a list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python List&nbsp;<code>sort()<\/code>&nbsp;method<\/h2>\n\n\n\n<p>To sort a\u00a0list, you use the\u00a0<code>sort()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>list.sort()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>sort()<\/code>&nbsp;method sorts the original list in place. It means that the&nbsp;<code>sort()<\/code>&nbsp;method modifies the order of elements in the list.<\/p>\n\n\n\n<p>By default, the&nbsp;<code>sort()<\/code>&nbsp;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\u00a0<code>reverse=True<\/code>\u00a0argument to the\u00a0<code>sort()<\/code>\u00a0method like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>list.sort(reverse=True)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python List sort() method examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the&nbsp;<code>sort()<\/code>&nbsp;method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using the Python List sort() method to sort a list of strings<\/h3>\n\n\n\n<p>If a list contains strings, the&nbsp;<code>sort()<\/code>&nbsp;method sorts the string elements alphabetically.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>sort()<\/code>\u00a0method to sort the elements in the\u00a0<code>guests<\/code>\u00a0list alphabetically:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>guests = &#91;'James', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer'] guests.sort() print(guests)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'James', 'Jennifer', 'John', 'Mary', 'Patricia', 'Robert']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And the following example uses the\u00a0<code>sort()<\/code>\u00a0method with the\u00a0<code>reverse=True<\/code>\u00a0argument to sort the elements in the\u00a0<code>guests<\/code>\u00a0list in the reverse alphabetical order:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>guests = &#91;'James', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer'] guests.sort(reverse=True) print(guests)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'Robert', 'Patricia', 'Mary', 'John', 'Jennifer', 'James']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using the Python List sort() method to sort a list of numbers<\/h3>\n\n\n\n<p>If a list contains numbers, the&nbsp;<code>sort()<\/code>&nbsp;method sorts the numbers from smallest to largest.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>sort()<\/code>\u00a0method to sort numbers in the\u00a0<code>scores<\/code>\u00a0list from smallest to largest:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>scores = &#91;5, 7, 4, 6, 9, 8] scores.sort() print(scores)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;4, 5, 6, 7, 8, 9]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To sort numbers from the largest to smallest, you use the\u00a0<code>sort(reverse=True)<\/code>\u00a0like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>scores = &#91;5, 7, 4, 6, 9, 8] scores.sort(reverse=True) print(scores)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;9, 8, 7, 6, 5, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using the Python List sort() method to sort a list of tuples<\/h3>\n\n\n\n<p>Suppose that you have a list of tuples like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>companies = &#91;('Google', 2019, 134.81), ('Apple', 2019, 260.2), ('Facebook', 2019, 70.7)]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\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\u00a0<code>sort()<\/code>\u00a0method. 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\n<pre class=\"wp-block-code\"><code><code>def sort_key(company): return company&#91;2]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This&nbsp;<code>sort_key()<\/code>&nbsp;function accepts a tuple called&nbsp;<code>company<\/code>&nbsp;and returns the third element.<\/p>\n\n\n\n<p>Note that the company is a tuple e.g.,&nbsp;<code>('Google', 2019, 134.81)<\/code>. And the&nbsp;<code>company[2]<\/code>&nbsp;references the revenue like&nbsp;<code>134.81<\/code>&nbsp;in this case.<\/p>\n\n\n\n<p>Second, pass the sort_key function to the\u00a0<code>sort()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>companies.sort(key=sort_key, reverse=True)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>sort()<\/code>&nbsp;method will use the value returned by the&nbsp;<code>sort_key()<\/code>&nbsp;function for the comparisons.<\/p>\n\n\n\n<p>Note that you just pass the function name without the parentheses to the\u00a0<code>sort()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>companies = &#91;('Google', 2019, 134.81), ('Apple', 2019, 260.2), ('Facebook', 2019, 70.7)] <em># define a sort key<\/em> def sort_key(company): return company&#91;2] <em># sort the companies by revenue<\/em> companies.sort(key=sort_key, reverse=True) <em># show the sorted companies<\/em> print(companies) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;('Apple', 2019, 260.2), ('Google', 2019, 134.81), ('Facebook', 2019, 70.7)]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using lambda expression<\/h4>\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\n<pre class=\"wp-block-code\"><code><code>lambda arguments: expression<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>A function without a name is called an\u00a0<strong>anonymous function<\/strong>. And this syntax is called a\u00a0lambda expression.<\/p>\n\n\n\n<p>Technically, it\u2019s equivalent to the following function:<code>def name(arguments): return expression<\/code><small>Code language: Python (python)<\/small><\/p>\n\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\n<pre class=\"wp-block-code\"><code><code>companies = &#91;('Google', 2019, 134.81), ('Apple', 2019, 260.2), ('Facebook', 2019, 70.7)] <em># sort the companies by revenue<\/em> companies.sort(key=lambda company: company&#91;2]) <em># show the sorted companies<\/em> print(companies)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;('Apple', 2019, 260.2), ('Google', 2019, 134.81), ('Facebook', 2019, 70.7)]<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python List&nbsp;sort()&nbsp;method to sort a list. Introduction to the Python List&nbsp;sort()&nbsp;method To sort a\u00a0list, you use the\u00a0sort()\u00a0method: The&nbsp;sort()&nbsp;method sorts the original list in place. It means that the&nbsp;sort()&nbsp;method modifies the order of elements in the list. By default, the&nbsp;sort()&nbsp;method sorts the elements of a list [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-5-lists"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/68\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}