{"id":24729,"date":"2021-11-02T09:45:47","date_gmt":"2021-11-02T04:15:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=24729"},"modified":"2021-11-05T17:22:53","modified_gmt":"2021-11-05T11:52:53","slug":"python-list-sort-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-sort-method-with-examples\/","title":{"rendered":"Python List sort() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python List reverse() Method with Examples<\/a> Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n List sort() Method in Python:<\/strong><\/p>\n The sort() method sorts the elements of a list in ascending or descending order.<\/p>\n By default, the sort() method ascends the list.<\/p>\n Syntax:<\/strong><\/p>\n Alternatively, you can use Python’s built-in sorted() function to accomplish the same thing.<\/p>\n Note:<\/strong> The most basic distinction between sort() and sorted() is that sort() changes the list directly and returns no value, whereas sorted() does not change the list and returns the sorted list.<\/p>\n Parameter Values of sort():<\/strong><\/p>\n Sort() does not require any additional parameters by default. It does, however, have two optional parameters:<\/p>\n Return Value:<\/strong><\/p>\n The sort() method produces no output. Instead, it modifies the original list.<\/p>\n Use sorted if you want a function to return the sorted list rather than change the original list ().<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you want to create your own sorting implementation, the sort() method accepts a key function as an optional parameter.<\/p>\n You can sort the given list based on the results of the key function.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Covered all examples on python concepts and missed checking out the Python List Method Examples<\/a> then go with the available link & gain more knowledge about List method concepts in python.<\/p>\n In the previous article, we have discussed Python List reverse() Method with Examples List in Python: Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of …<\/p>\n
\nList in Python:<\/strong><\/p>\nlist.sort(key=...., reverse=....)<\/pre>\n
sorted(list, key=...., reverse=....)<\/pre>\n
\n
Given List = ['g', 'r', 'c', 'x', 'e']<\/pre>\n
The Given original list is : ['g', 'r', 'c', 'x', 'e']\r\nThe given list after sorting in ascending order: ['c', 'e', 'g', 'r', 'x']<\/pre>\n
Given List = ['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
The Given original list is : ['hello', 'this', 'is', 'btechgeeks']\r\nThe given list after sorting in descending order: \r\n['this', 'is', 'hello', 'btechgeeks']<\/pre>\n
List sort() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1) sort in Ascending Order<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['g', 'r', 'c', 'x', 'e']\r\n# Print the given original list.\r\nprint(\"The Given original list is :\", gvn_lst)\r\n# Apply sort() function to the given list to sort the given list elements in\r\n# ascending order.\r\ngvn_lst.sort()\r\n# Print the list after sorting all its elements in ascending order.\r\nprint(\"The given list after sorting in ascending order: \", gvn_lst)\r\n<\/pre>\n
The Given original list is : ['g', 'r', 'c', 'x', 'e']\r\nThe given list after sorting in ascending order: ['c', 'e', 'g', 'r', 'x']<\/pre>\n
2) sort in Descending Order<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"hello\", \"this\", \"is\", \"btechgeeks\"]\r\n# Print the given original list.\r\nprint(\"The Given original list is :\", gvn_lst)\r\n# Apply sort() function to the given list by passing the argument as\r\n# reverse = \"True\" to sort the given list elements in descending order.\r\ngvn_lst.sort(reverse=True)\r\n# Print the list after sorting all its elements in descending order.\r\nprint(\"The given list after sorting in descending order: \")\r\nprint(gvn_lst)\r\n<\/pre>\n
The Given original list is : ['hello', 'this', 'is', 'btechgeeks']\r\nThe given list after sorting in descending order: \r\n['this', 'is', 'hello', 'btechgeeks']<\/pre>\n
3)Sort with custom function using key<\/h5>\n
list.sort(key=len)<\/code><\/pre>\n
# Taking element to sort\r\ndef givenelement(element):\r\n return element[0]\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [(1, 3), (4, 6), (2, 6), (5, 1)]\r\n# sort the given list with the key\r\ngvn_lst.sort(key=givenelement)\r\n# print the given list\r\nprint('The given list after Sorting:', gvn_lst)\r\n<\/pre>\n
The given list after Sorting: [(1, 3), (2, 6), (4, 6), (5, 1)]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1) sort in Ascending Order<\/h5>\n
\n
# Give the list as user input using list(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(input(\"Enter some random List Elements separated by spaces = \").split())\r\n# Print the given original list.\r\nprint(\"The Given original list is :\", gvn_lst)\r\n# Apply sort() function to the given list to sort the given list elements in\r\n# ascending order.\r\ngvn_lst.sort()\r\n# Print the list after sorting all its elements in ascending order.\r\nprint(\"The given list after sorting in ascending order: \", gvn_lst)<\/pre>\n
Enter some random List Elements separated by spaces = A R Q D I\r\nThe Given original list is : ['A', 'R', 'Q', 'D', 'I']\r\nThe given list after sorting in ascending order: ['A', 'D', 'I', 'Q', 'R']<\/pre>\n
2) sort in Descending Order<\/h5>\n
\n
# Give the list as user input using list(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(input(\"Enter some random List Elements separated by spaces = \").split())\r\n# Print the given original list.\r\nprint(\"The Given original list is :\", gvn_lst)\r\n# Apply sort() function to the given list by passing the argument as\r\n# reverse = \"True\" to sort the given list elements in descending order.\r\ngvn_lst.sort(reverse=True)\r\n# Print the list after sorting all its elements in descending order.\r\nprint(\"The given list after sorting in descending order: \")\r\nprint(gvn_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = good morning btechgeeks hello all\r\nThe Given original list is : ['good', 'morning', 'btechgeeks', 'hello', 'all']\r\nThe given list after sorting in descending order: \r\n['morning', 'hello', 'good', 'btechgeeks', 'all']<\/pre>\n
\n