{"id":294,"date":"2020-10-06T04:18:27","date_gmt":"2020-10-06T04:18:27","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=294"},"modified":"2025-03-26T13:47:57","modified_gmt":"2025-03-26T13:47:57","slug":"python-sorted","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-sorted\/","title":{"rendered":"Python sorted"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python <code>sorted()<\/code> function to sort a list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-sorted-function'>Introduction to the Python sorted() function <a href=\"#introduction-to-the-python-sorted-function\" class=\"anchor\" id=\"introduction-to-the-python-sorted-function\" title=\"Anchor for Introduction to the Python sorted() function\">#<\/a><\/h2>\n\n\n\n<p>The <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-sort-list\/\">sort()<\/a><\/code> method sorts a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> in 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 <code>sorted()<\/code> function:<\/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\">sorted(list)<\/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>sorted()<\/code> function doesn&#8217;t modify the original list.<\/p>\n\n\n\n<p>By default, the <code>sorted()<\/code> function sorts the elements of the list from lowest to highest using the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-comparison-operators\/\">less-than operator<\/a> (<code>&lt;<\/code>).<\/p>\n\n\n\n<p>If you want to reverse the sort order, you pass the <code>reverse<\/code> argument as <code>True<\/code> 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\">sorted(list,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>The following example uses the <code>sorted()<\/code> function to sort a list of strings in alphabetical order:<\/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>]\nsorted_guests = sorted(guests)\n\nprint(guests)\nprint(sorted_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=Z3Vlc3RzID0gWydKYW1lcycsICdNYXJ5JywgJ0pvaG4nLCAnUGF0cmljaWEnLCAnUm9iZXJ0JywgJ0plbm5pZmVyJ10Kc29ydGVkX2d1ZXN0cyA9IHNvcnRlZChndWVzdHMpCgpwcmludChndWVzdHMpCnByaW50KHNvcnRlZF9ndWVzdHMp\" 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\">'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>]\n&#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>The output indicates the original list doesn&#8217;t change. The <code>sorted()<\/code> method returns a new sorted list from the original list.<\/p>\n\n\n\n<p>The following example uses the <code>sorted()<\/code> function to sort 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>]\nsorted_guests = sorted(guests, reverse=<span class=\"hljs-literal\">True<\/span>)\n\nprint(sorted_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=Z3Vlc3RzID0gWydKYW1lcycsICdNYXJ5JywgJ0pvaG4nLCAnUGF0cmljaWEnLCAnUm9iZXJ0JywgJ0plbm5pZmVyJ10Kc29ydGVkX2d1ZXN0cyA9IHNvcnRlZChndWVzdHMsIHJldmVyc2U9VHJ1ZSkKCnByaW50KHNvcnRlZF9ndWVzdHMp\" 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<h3 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><\/h3>\n\n\n\n<p>The following example uses the <code>sorted()<\/code> function to sort a list of numbers 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>]\nsorted_scores = sorted(scores)\n\nprint(sorted_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=c2NvcmVzID0gWzUsIDcsIDQsIDYsIDksIDhdCnNvcnRlZF9zY29yZXMgPSBzb3J0ZWQoc2NvcmVzKQoKcHJpbnQoc29ydGVkX3Njb3Jlcyk%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>The following example uses the <code>sorted()<\/code> function with the <code>reverse<\/code> argument sets to <code>True<\/code>. It sorts a list of numbers from largest to smallest:<\/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>]\nsorted_scores = sorted(scores, reverse=<span class=\"hljs-literal\">True<\/span>)\n\nprint(sorted_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=c2NvcmVzID0gWzUsIDcsIDQsIDYsIDksIDhdCnNvcnRlZF9zY29yZXMgPSBzb3J0ZWQoc2NvcmVzLCByZXZlcnNlPVRydWUpCgpwcmludChzb3J0ZWRfc2NvcmVzKQ%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-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<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 <code>sorted()<\/code> function to return a new sorted list from a list.<\/li>\n\n\n\n<li>Use the <code>sorted()<\/code> function with the <code>reverse<\/code> argument sets to <code>True<\/code> to sort a list in the reverse 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-sorted\"\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=\"294\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-sorted\/\"\n\t\t\t\tdata-post-title=\"Python sorted\"\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=\"294\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-sorted\/\"\n\t\t\t\tdata-post-title=\"Python sorted\"\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 sorted() function to sort a list. Introduction to the Python sorted() function # The sort() method sorts a list in place. In other words, it changes the order of elements in the original list. To return the new sorted list from the original list, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":28,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-294","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/294","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=294"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/294\/revisions"}],"predecessor-version":[{"id":7018,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/294\/revisions\/7018"}],"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=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}