Python sorted() FunctionPython sorted() function is used to sort a sequence of elements. The sorted() function can be used to sort a variety of data structures or complex objects effortlessly in ascending or descending order. By default, it sorts elements in ascending order. The sorted function takes three arguments and returns a list in sorted order. In the case of a dictionary, it returns a list of sorted keys. The time complexity of the sorted function is O(n logn). The sorted function does not sort the passed sequence but creates a new sorted list. To sort a sequence, use the sort function rather than the sorted function. In this article, we will explore the complexities, applications, advantages, and utility of sorted function in different scenarios. The signature or syntax of the sorted function is given below: Signature or SyntaxParameters
ReturnIt returns a new list containing the elements in a sorted order. Let's see some examples of sorted() function to understand its functionality. Python sorted() Function Example 1 - Sorting string elementsHere, in this example, we are sorting a string object to understand the idea behind the sorted function. Output: Sorted String Elements: ['a', 'a', 'i', 'j', 'n', 'o', 'p', 't', 't', 'v'] Explanation: In the above example, the sorted function sorts each alphabet in the string "javatpoint" alphabetically and returns a list containing elements in a sorted manner. Python sorted() Function Example 2 - Sorting list, tuple, and dictionary elements using sorted functionWe can use the sorted function to sort any iterable, like a list, tuple, or dictionary. See the example below. Output: Sorted List elements: [56, 98, 622, 659, 1002, 2003, 3652] Sorted Tuple Elements: [21, 232, 2500, 2578, 3698, 5264] Sorted Dictionary Keys: [1, 2, 3, 4] Explanation: In the above example, the sorted function sorts the elements of the list and tuple and returns separate sorted lists. In the case of the dictionary, it sorts the keys only and returns a list containing keys in a sorted manner. Python sorted() Function Example 3 - Sorting list elements in reverse order using the sorted functionTo sort the list into reverse order (descending), pass True in the reverse parameter, and we will get the list sorted in reverse order. Output: Sorted list elements: [3652, 2003, 1002, 659, 622, 98, 56] Explanation: In the above example, we have passed Ture in the reverse argument that sorts the list elements in reverse or descending order and returns a list containing elements in descending order. Python sorted() Function Example 4 - Exploring Key Argument with Lambda FunctionThe key parameter is used to customize the sorting criteria. The key parameter takes a function that produces a key known as a comparison key for each element. Here, what happens is that the key function takes each element from the iterable and generates a corresponding comparison key for that element based on the function passed. The sorted function uses those comparison keys during the sorting process. The key argument is set to None by default. For example, when you required to sort a list of strings based on the length of the string. You can pass the 'len' function as the key parameter. Here, we are sorting a list of tuples by passing a lambda function in the key during the call, which returns the sum of the elements for each tuple. Output: Sorted list of tuples: [(3, 5), (8, 5), (2, 15), (65, 5)] Explanation: In the above example, the sorted function sorts the elements (tuples) based on the sum of its elements. The lambda function takes each element (tuple) from the key and returns the corresponding sum values as the comparison keys. The sorted function uses those keys to sort the elements of the list. Python sorted() Function Example 5 - Exploring Key Argument with Simple FunctionIn this example, we are passing a sum function instead of a lambda function as the key argument in the same example. Output: Sorted list of tuples: [(3, 5), (8, 5), (2, 15), (65, 5)] Explanation: In Python, the sum is an inbuilt function that returns the sum of values. In the above example, the key parameter takes each tuple from the list and passes it to the sum function. The sum function returns the corresponding sum value of each tuple. The sorted function then uses these sum values as a comparison key during the sorting process. Advantages of the sorted function:Here are some potential advantages of the sorted function below:
Applications of the sorted function:Some of the common applications of sorted functions are listed below:
CONCLUSION:With the help of Python's sorted() function, we may quickly and effectively sort a variety of data structures. It may be used for various sorting circumstances, from simple lists and strings to complex objects, thanks to its flexibility and simplicity. We may customize the sorting process to meet our particular demands by using the key parameter. The sorted() function in Python provides a dependable and simple method for organizing our data, making it an invaluable tool for any coders. Next TopicPython Set |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India