What is sort () in python?
The Show
Example
sort() SyntaxThe syntax of the list.sort(key=..., reverse=...) Alternatively, you can also use Python's built-in sorted() function for the same purpose. sorted(list, key=..., reverse=...) Note: The simplest difference between sort() ParametersBy default,
sort() Return ValueThe
If you want a function to return the sorted list rather than change the original list, use Example 1: Sort a given list
Output Sorted list: ['a', 'e', 'i', 'o', 'u'] Sort in Descending orderThe Setting
Alternatively for
Example 2: Sort the list in Descending order
Output Sorted list (in Descending): ['u', 'o', 'i', 'e', 'a'] Sort with custom function using keyIf you want your own implementation for sorting, the Based on the results of the key function, you can sort the given list.
Alternatively for sorted:
Here, The list is sorted based on the length of each element, from lowest count to highest. We know that a tuple is sorted using its first parameter by default. Let's look at how to customize the Example 3: Sort the list using key
Output Sorted list: [(4, 1), (2, 2), (1, 3), (3, 4)] Let's take another example. Suppose we have a list of information about the employees of an office where each element is a dictionary. We can sort the list in the following way:
Output [{'Name': 'Alan Turing', 'age': 25, 'salary': 10000}, {'Name': 'John Hopkins', 'age': 18, 'salary': 1000}, {'Name': 'Mikhail Tal', 'age': 40, 'salary': 15000}, {'Name': 'Sharon Lin', 'age': 30, 'salary': 8000}] [{'Name': 'John Hopkins', 'age': 18, 'salary': 1000}, {'Name': 'Alan Turing', 'age': 25, 'salary': 10000}, {'Name': 'Sharon Lin', 'age': 30, 'salary': 8000}, {'Name': 'Mikhail Tal', 'age': 40, 'salary': 15000}] [{'Name': 'Mikhail Tal', 'age': 40, 'salary': 15000}, {'Name': 'Alan Turing', 'age': 25, 'salary': 10000}, {'Name': 'Sharon Lin', 'age': 30, 'salary': 8000}, {'Name': 'John Hopkins', 'age': 18, 'salary': 1000}] Here, for the first case, our custom function returns the name of each employee. Since the name is a For the second case, age ( For the third case, the function returns the salary ( It is a good practice to use the lambda function when the function can be summarized in one line. So, we can also write the above program as:
Output [{'Name': 'Alan Turing', 'age': 25, 'salary': 10000}, {'Name': 'John Hopkins', 'age': 18, 'salary': 1000}, {'Name': 'Mikhail Tal', 'age': 40, 'salary': 15000}, {'Name': 'Sharon Lin', 'age': 30, 'salary': 8000}] [{'Name': 'John Hopkins', 'age': 18, 'salary': 1000}, {'Name': 'Alan Turing', 'age': 25, 'salary': 10000}, {'Name': 'Sharon Lin', 'age': 30, 'salary': 8000}, {'Name': 'Mikhail Tal', 'age': 40, 'salary': 15000}] [{'Name': 'Mikhail Tal', 'age': 40, 'salary': 15000}, {'Name': 'Alan Turing', 'age': 25, 'salary': 10000}, {'Name': 'Sharon Lin', 'age': 30, 'salary': 8000}, {'Name': 'John Hopkins', 'age': 18, 'salary': 1000}] Recommended Readings
How does sort () sort in Python?The sorted() function returns a sorted list of the specific iterable object. The sort() method sorts the list.. We can specify ascending or descending order while using the sorted() function. It sorts the list in ascending order by default.. Its syntax is : ... . Its return type is a sorted list.. What is the sort () method?The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
What is sort in list in Python?sort() is one of Python's list methods for sorting and changing a list. It sorts list elements in either ascending or descending order. sort() accepts two optional parameters. reverse is the first optional parameter. It specifies whether the list will be sorted in ascending or descending order.
What is the difference between sort () and sorted ()?sort() will sort the list in-place, mutating its indexes and returning None , whereas sorted() will return a new sorted list leaving the original list unchanged. Another difference is that sorted() accepts any iterable while list. sort() is a method of the list class and can only be used with lists.
|