{"id":70,"date":"2024-01-25T09:34:51","date_gmt":"2024-01-25T09:34:51","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=70"},"modified":"2024-01-25T09:34:53","modified_gmt":"2024-01-25T09:34:53","slug":"python-sorted","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-sorted\/","title":{"rendered":"Python sorted"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>sorted()<\/code>&nbsp;function to sort a list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python sorted() function<\/h2>\n\n\n\n<p>The\u00a0<code>sort()<\/code>\u00a0method sorts a\u00a0list\u00a0in place. In other words, it changes the order of elements in the original list.<\/p>\n\n\n\n<p>To return the new sorted list from the original list, you use the\u00a0<code>sorted()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sorted(list)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>sorted()<\/code>&nbsp;function doesn\u2019t modify the original list.<\/p>\n\n\n\n<p>By default, the\u00a0<code>sorted()<\/code>\u00a0function sorts the elements of the list from lowest to highest using the\u00a0less-than operator\u00a0(<code>&lt;<\/code>).<\/p>\n\n\n\n<p>If you want to reverse the sort order, you pass the\u00a0<code>reverse<\/code>\u00a0argument as\u00a0<code>True<\/code>\u00a0like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sorted(list,reverse=True)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python sorted() function examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the Python&nbsp;<code>sorted()<\/code>&nbsp;function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using Python sorted() function to sort a list of strings<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>sorted()<\/code>\u00a0function to sort a list of strings in alphabetical order:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>guests = &#91;'James', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer'] sorted_guests = sorted(guests) print(guests) print(sorted_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', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer'] &#91;'James', 'Jennifer', 'John', 'Mary', 'Patricia', 'Robert']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>As you can see clearly in the output, the original list doesn\u2019t change. The&nbsp;<code>sorted()<\/code>&nbsp;method returns a new sorted list from the original list.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>sorted()<\/code>\u00a0function to sort 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'] sorted_guests = sorted(guests, reverse=True) print(sorted_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 Python sorted() function to sort a list of numbers<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>sorted()<\/code>\u00a0function to sort a list of numbers from smallest to largest:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>scores = &#91;5, 7, 4, 6, 9, 8] sorted_scores = sorted(scores) print(sorted_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>The following example uses the\u00a0<code>sorted()<\/code>\u00a0function with the\u00a0<code>reverse<\/code>\u00a0argument sets to\u00a0<code>True<\/code>. It sorts a list of numbers from largest to smallest:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>scores = &#91;5, 7, 4, 6, 9, 8] sorted_scores = sorted(scores, reverse=True) print(sorted_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><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;sorted()&nbsp;function to sort a list. Introduction to the Python sorted() function The\u00a0sort()\u00a0method sorts a\u00a0list\u00a0in place. In other words, it changes the order of elements in the original list. To return the new sorted list from the original list, you use the\u00a0sorted()\u00a0function: The&nbsp;sorted()&nbsp;function doesn\u2019t modify the [&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-70","post","type-post","status-publish","format-standard","hentry","category-5-lists"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/70","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=70"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/70\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}