{"id":2548,"date":"2020-08-06T02:05:44","date_gmt":"2020-08-06T02:05:44","guid":{"rendered":"https:\/\/www.afternerd.com\/blog\/?p=2548"},"modified":"2020-08-06T02:05:45","modified_gmt":"2020-08-06T02:05:45","slug":"sort-vs-sorted","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/sort-vs-sorted\/","title":{"rendered":"Python: Sort vs Sorted"},"content":{"rendered":"\n<p> In Python, you can use <strong>sort<\/strong> or <strong>sorted<\/strong> to sort a list.<\/p>\n\n\n\n<p>But what is the difference between the two? This is what I will discuss in this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">First: Sorting a Python List using Sort<\/h2>\n\n\n\n<p><span class=\"symbol\">sort()<\/span> is a <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/tutorial\/classes.html#:~:text=A%20method%20is%20a%20function,%2C%20sort%2C%20and%20so%20on.\" target=\"_blank\">method<\/a> on the list object.<\/p>\n\n\n\n<p>Say, you have an arbitrary list <span class=\"symbol\">L<\/span> and you want to sort it. Here is what you can do.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> L = &#091;5, 3, 7]\n>>> L.sort()\n>>> L\n&#091;3, 5, 7]<\/code><\/pre>\n\n\n\n<p>There is one thing that I want to notice: L was sorted in-place. This means that no <em>new<\/em> lists were created.<\/p>\n\n\n\n<p>You can double check that using the <span class=\"symbol\">id<\/span> built-in function.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> L = &#091;5, 3, 7]\n>>> id(L)\n4477543040\n>>> L.sort()\n>>> L\n&#091;3, 5, 7]\n>>> id(L)\n4477543040<\/code><\/pre>\n\n\n\n<p>Having the same id means that the location of the object is the same before and after sorting, which confirms that sorting happened in-place.<\/p>\n\n\n\n<p>Now let&#8217;s talk about sorted().<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Second: Sorting a Python List using Sorted<\/h2>\n\n\n\n<p>Unlike sort, sorted is not a method.<\/p>\n\n\n\n<p><strong>sorted()<\/strong> is a built-in function like <strong>print()<\/strong> or <strong>id()<\/strong>.<\/p>\n\n\n\n<p>Let&#8217;s try to use sorted to sort the same list from above.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> L = &#091;5, 3, 7]\n>>> sorted(L)\n&#091;3, 5, 7]\n>>> L\n&#091;5, 3, 7]<\/code><\/pre>\n\n\n\n<p>It doesn&#8217;t seem to work, does it? It looks like <strong>L<\/strong> hasn&#8217;t changed. Why is that?<\/p>\n\n\n\n<p>Unlike sort, sorted creates a <em>new list object<\/em> and sorts the elements of the original list <strong>L<\/strong> in this new object without modifying the original list.<\/p>\n\n\n\n<p>This is an essential difference between sort and sorted.<\/p>\n\n\n\n<p>You can easily confirm that by inspecting the ids.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> L = &#091;5, 3, 7]\n>>> id(L)\n4477542848\n>>> id(sorted(L))\n4477542976<\/code><\/pre>\n\n\n\n<p>So with that in mind, the correct way to use sorted is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> L = &#091;5, 3, 7]\n>>> Q = sorted(L)\n>>> Q\n&#091;3, 5, 7]<\/code><\/pre>\n\n\n\n<p class=\"after-post-box\">So essentially, if you want to sort the elements of a list in place, use the <strong>sort<\/strong> method. But if you want to sort the items of a list in a new list object without modifying the original list, use the <strong>sorted<\/strong> built-in function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What sorting algorithm is used for sort and sorted?<\/h2>\n\n\n\n<p>Both <strong>sort<\/strong> and <strong>sorted<\/strong> use the\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Timsort\" target=\"_blank\">Timsort<\/a>\u00a0sorting algorithm which is a hybrid sorting algorithm between <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Merge_sort\" target=\"_blank\">merge sort<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Insertion_sort\" target=\"_blank\">insertion sort<\/a>.<\/p>\n\n\n\n<p>If you want to know more about Timsort and its superior performance, then I <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/python\/cpython\/blob\/24e5ad4689de9adc8e4a7d8c08fe400dcea668e6\/Objects\/listsort.txt\" target=\"_blank\">highly recommend reading this<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Questions<\/h2>\n\n\n\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/python-sort-list\/#sort-numbers\">How to sort a list of numbers? (ascendignly or descendignly)\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/python-sort-list\/#sort-strings\">How to sort a list of strings<\/a>\n<ul>\n<li><a href=\"#https:\/\/www.afternerd.com\/blog\/python-sort-list\/sort-strings-case-insensitive\">How to sort a list strings in a case insensitive manner<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/python-sort-list\/#sort-tuples\">How to sort a list of tuples?<\/a>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/python-sort-list\/#sort-tuples-second-element\">How to sorting a list of tuples by the second element?<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/python-sort-list\/#sort-objects\">How to sort a list of arbitrary objects?<\/a><\/li>\n<\/ul>\n\n\n<h3>Learning Python?<\/h3>\n<p>Check out <a href=\"https:\/\/courses.afternerd.com\/\">the Courses section!<\/a><\/p>\n\n\n\n<h2>Featured Posts<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-python\/\">The Python Learning Path (From Beginner to Mastery)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-computer-science\/\">Learn Computer Science (From Zero to Hero)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/coding-interview\/\">Coding Interview Preparation Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/stock-investing-for-beginners\/\">The Programmer&#8217;s Guide to Stock Market Investing<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/start-programming-blog\/\">How to Start Your Programming Blog?<\/a><\/li>\n<\/ul>\n<div class=\"after-post-box\">\n<h2>Are you Beginning your Programming Career?<\/h2>\n<h3>I provide my best content for beginners in the newsletter.<\/h3>\n<ul>\n<li>Python tips for beginners, intermediate, and advanced levels.<\/li>\n<li>CS Career tips and advice.<\/li>\n<li>Special discounts on my premium courses when they launch.<\/li>\n<\/ul>\n<p>And so much more&#8230;<\/p>\n<h3>Subscribe now. It&#8217;s Free.<\/h3>\n<p><script type=\"text\/javascript\" src=\"\/\/mautic.afternerd.com\/form\/generate.js?id=2\"><\/script><\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":2561,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[1670,321,586,1680,1679,514,1681,229,1677,1678,1673,240,558,582,1672,1671,563,1668,1669,1682],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2548"}],"collection":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/comments?post=2548"}],"version-history":[{"count":12,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2548\/revisions"}],"predecessor-version":[{"id":2560,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2548\/revisions\/2560"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/2561"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=2548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=2548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=2548"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}