{"id":8100,"date":"2024-03-19T06:53:49","date_gmt":"2024-03-19T06:53:49","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8100"},"modified":"2024-03-19T06:53:49","modified_gmt":"2024-03-19T06:53:49","slug":"python-list-sort-method","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-list-sort-method\/","title":{"rendered":"Python List sort() Method"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-the-python-list-sort-method\">What is the Python List sort () Method?<\/a><\/li><li ><a href=\"#what-is-the-syntax-of-the-python-list-sort\">What is the Syntax of the Python List Sort()?<\/a><\/li><li ><a href=\"#how-to-use-the-list-sort-function\">How to Use the List Sort () Function<\/a><\/li><li ><a href=\"#python-list-sort-examples-and-use\">Python list sort() Examples and Use<\/a><\/li><li ><a href=\"#what-are-python-list-sort-numbers-in-ascending-order\">What are Python List sort numbers in Ascending Order?<\/a><\/li><li ><a href=\"#what-is-sort-a-list-of-alphabets-in-the-ascending-order\">What is Sort a List of Alphabets in the Ascending Order?<\/a><\/li><li ><a href=\"#what-is-python-sort-list-in-the-descending-order\">What is Python Sort List in the Descending Order?<\/a><\/li><li ><a href=\"#python-sort-list-by-key\">Python sort List by Key\u00a0<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-list-sort-method-fa-qs\">Python List sort() Method-FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>The Python list sort () method will help to sort the elements of the list. Hence, it will sort the ascending order by default but will sort the values in the descending values. This article has listed the Python List Sort Method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-python-list-sort-method\">What is the Python List sort () Method?<\/h2>\n\n\n\n<p>Python list sort method will sort the elements of the list. The <a href=\"https:\/\/www.skillvertex.com\/blog\/python-remove-list-items\/\" data-type=\"post\" data-id=\"8071\">list <\/a>sort() is referred to as the in-build function in Python and will sort the values of the list in ascending or descending order. Whereas, by default, it will then sort the values in ascending order. So, it is referred to as a very useful and simple list operation in <a href=\"http:\/\/python\" rel=\"nofollow\" target=\"_blank\">Python<\/a>. <\/p>\n\n\n\n<p>Let us look into the example provided below<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example list\nmy_list = &#91;3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]\n\n# Using the sort() method\nmy_list.sort()\n\n# Displaying the sorted list\nprint(\"Sorted List:\", my_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted List: &#91;1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-syntax-of-the-python-list-sort\">What is the Syntax of the Python List Sort()?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>List_name.sort(reverse=True\/False, key=myFunc)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-the-list-sort-function\">How to Use the List Sort () Function<\/h2>\n\n\n\n<p>The list sort <a href=\"https:\/\/www.skillvertex.com\/blog\/static-functions-in-c\/\" data-type=\"post\" data-id=\"3038\">function <\/a>in Python will be very easy. Hence, it is required to call the function with the list <a href=\"https:\/\/www.skillvertex.com\/blog\/which-is-not-an-ado-net-dataadapter-object\/\" data-type=\"post\" data-id=\"2225\">object<\/a>. Parameters can be used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-list-sort-examples-and-use\">Python list sort() Examples and Use<\/h2>\n\n\n\n<p>The use case scenarios of the list sort() method with the examples. Some of the examples are given below:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Sort a List of Numbers in Ascending Order<\/li>\n\n\n\n<li>Sort a List of Alphabets In Ascending Order<\/li>\n\n\n\n<li>Sort a List in Python in Descending Order&nbsp;<\/li>\n\n\n\n<li>Sort a List in Python By Key<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-python-list-sort-numbers-in-ascending-order\">What are Python List sort numbers in Ascending Order?<\/h2>\n\n\n\n<p>The sort() method by default will sort <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-an-element-of-iot\/\" data-type=\"post\" data-id=\"3089\">elements <\/a>in ascending order.  Check the example given below:<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Sample list of numbers\nnumbers = &#91;5, 2, 8, 1, 3]\n\n# Sorting the list in ascending order\nnumbers.sort()\n\n# Displaying the sorted list\nprint(\"Sorted Numbers in Ascending Order:\", numbers)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted Numbers in Ascending Order: &#91;1, 2, 3, 5, 8]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-sort-a-list-of-alphabets-in-the-ascending-order\">What is Sort a List of Alphabets in the Ascending Order?<\/h2>\n\n\n\n<p>The sort <a href=\"https:\/\/www.skillvertex.com\/blog\/python-string-methods\/\" data-type=\"post\" data-id=\"7928\">method<\/a> will sort the list in the order from the A-Z in the alphabet.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Sample list of alphabets\nalphabets = &#91;'c', 'a', 'b', 'f', 'e', 'd']\n\n# Sorting the list in ascending order\nalphabets.sort()\n\n# Displaying the sorted list\nprint(\"Sorted Alphabets in Ascending Order:\", alphabets)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted Alphabets in Ascending Order: &#91;'a', 'b', 'c', 'd', 'e', 'f']\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-sort-list-in-the-descending-order\">What is Python Sort List in the Descending Order?<\/h2>\n\n\n\n<p>In Python, it is possible to sort the list of <a href=\"https:\/\/www.skillvertex.com\/blog\/python-numbers\/\" data-type=\"post\" data-id=\"7023\">numbers <\/a>in descending order and the same will be for alphabets. Thus, to do this, we need to pass the  reverse=True and it will sort the numbers or the alphabet in descending order.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Sample list of numbers\nnumbers = &#91;5, 2, 8, 1, 3]\n\n# Sorting the list in descending order\nnumbers.sort(reverse=True)\n\n# Displaying the sorted list\nprint(\"Sorted Numbers in Descending Order:\", numbers)\n<\/code><\/pre>\n\n\n\n<p> Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted Numbers in Descending Order: &#91;8, 5, 3, 2, 1]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-sort-list-by-key\">Python sort List by Key&nbsp;<\/h2>\n\n\n<div class=\"gb-container gb-container-2ac4e8ea\">\n\n<p>We will sort the elements with the help of a <a href=\"https:\/\/www.skillvertex.com\/blog\/c-fopen-function-with-examples\/\" data-type=\"post\" data-id=\"3556\">function<\/a> and are based on passing the function to the key parameter of the sort() function.<\/p>\n\n<\/div>\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Sample list of dictionaries\npeople = &#91;\n    {'name': 'John', 'age': 30},\n    {'name': 'Alice', 'age': 25},\n    {'name': 'Bob', 'age': 35},\n]\n\n# Sorting the list of dictionaries by the 'age' key\nsorted_people = sorted(people, key=lambda x: x&#91;'age'])\n\n# Displaying the sorted list\nprint(\"Sorted People by Age:\", sorted_people)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted People by Age: &#91;{'name': 'Alice', 'age': 25}, {'name': 'John', 'age': 30}, {'name': 'Bob', 'age': 35}]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In conclusion, the sort() method in Python is a straightforward and convenient way to sort a list in place. It arranges the elements of the list in ascending order by default but can be customized for descending order by providing the reverse=True parameter. <\/p>\n\n\n\n<p>Hence, this method will modify the original list and won&#8217;t create a new one. It is a useful tool for quickly organizing numerical or alphabetical data within a list without the need for additional sorting functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-list-sort-method-fa-qs\">Python List sort() Method-FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1710330677364\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What does sort () do in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The sort() method will sort the list in the ascending order.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710330682449\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.How will the Python method sort () sort a list of strings?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The sorted() function will return the sorted list of the specified iterable object.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710330694717\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What is the use of the sort () function?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Sort will return the sorted array of the elements in the array.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The Python list sort () method will help to sort the elements of the list. Hence, it will sort the ascending order by default but will sort the values in the descending values. This article has listed the Python List Sort Method. What is the Python List sort () Method? Python list sort method will &#8230; <a title=\"Python List sort() Method\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-list-sort-method\/\" aria-label=\"More on Python List sort() Method\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,932,964,973,880],"class_list":["post-8100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-functions","tag-python-list","tag-python-list-sort-method","tag-python-syntax","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8100"}],"version-history":[{"count":6,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8100\/revisions"}],"predecessor-version":[{"id":8296,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8100\/revisions\/8296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8102"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}