What is sorting in python programming?
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
What is sorting in Python?The sort() method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.
What is sort in Python with example?Definition and Usage. The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.
What is sorting explain?What is sorting? Sorting is the process of arranging data into meaningful order so that you can analyze it more effectively.
What does sorting mean in programming?In computer science, arranging in an ordered sequence is called "sorting". Sorting is a common operation in many applications, and efficient algorithms to perform it have been developed. The most common uses of sorted sequences are: making lookup or search efficient; making merging of sequences efficient.
|