Python sorted() Function Last Updated : 08 Mar, 2025 Comments Improve Suggest changes Like Article Like Report sorted() function returns a new sorted list from the elements of any iterable like (e.g., list, tuples, strings ). It creates and returns a new sorted list and leaves the original iterable unchanged. Let's start with a basic example of sorting a list of numbers using the sorted() function. Python a = [4, 1, 3, 2] # Using sorted() to create a new sorted list without modifying the original list b = sorted(a) print(b) Output[1, 2, 3, 4] Table of ContentSyntax of sorted() functionSorting in ascending orderSorting in descending orderSorting using key parameterSyntax of sorted() functionsorted(iterable, key=None, reverse=False)Parameters:iterable: The sequence to be sorted. This can be a list, tuple, set, string, or any other iterable.key (Optional): A function to execute for deciding the order of elements. By default it is Nonereverse (Optional): If True, sorts in descending order. Defaults value is False (ascending order)Return Type:Returns a new list containing all elements from the given iterable, sorted according to the provided criteria.Sorting in ascending orderWhen no additional parameters are provided then It arranges the element in increasing order. Python a = [5, 2, 9, 1, 3] #Sorted() arranges the list in ascending order b = sorted(a) print(b) Output[1, 2, 3, 5, 9] Sorting in descending orderTo sort an iterable in descending order, set the reverse argument to True. Python a = [5, 2, 9, 1, 5, 6] # "reverse= True" helps sorted() to arrange the element #from largest to smallest elements res = sorted(a, reverse=True) print(res) Output[9, 6, 5, 5, 2, 1] Sorting using key parameterThe key parameter is an optional argument that allows us to customize the sort order.Sorting Based on String Length: Python a = ["apple", "banana", "cherry", "date"] res = sorted(a, key=len) print(res) Output['date', 'apple', 'banana', 'cherry'] Explanation: The key parameter is set to len, which sorts the words by their length in ascending order.Sorting a List of Dictionaries: Python a = [ {"name": "Alice", "score": 85}, {"name": "Bob", "score": 91}, {"name": "Eve", "score": 78} ] # Use sorted() to sort the list 'a' based on the 'score' key # sorted() returns a new list with dictionaries sorted by the 'score' b = sorted(a, key=lambda x: x['score']) print(b) Output[{'name': 'Eve', 'score': 78}, {'name': 'Alice', 'score': 85}, {'name': 'Bob', 'score': 91}] Explanation: key=lambda x: x['score'] specifies that the sorting should be done using the 'score' value from each dictionaryRelated Articles:Python Sort() MethodSort Python Dictionaries By Key or ValueHow to Sort a Set of Values?How to Sort a Dictionary by Value?Sorting Objects of User Defined Class Comment More infoAdvertise with us Next Article Python sorted() Function H haragr Follow Improve Article Tags : Python Python-Library python Practice Tags : pythonpython Similar Reads Python map() function The map() function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a map object (which is an iterator). Let's start with a simple example of using map() to convert a list of strings into a list of integers.Pythons = ['1', '2', '3', '4'] res = map( 4 min read Python - max() function Python max() function returns the largest item in an iterable or the largest of two or more arguments. It has two forms. max() function with objectsmax() function with iterablePython max() function With ObjectsUnlike the max() function of C/C++, the max() function in Python can take any type of obje 4 min read memoryview() in Python The memoryview() function in Python is used to create a memory view object that allows us to access and manipulate the internal data of an object without copying it. This is particularly useful for handling large datasets efficiently because it avoids the overhead of copying data. A memory view obje 5 min read Python min() Function Python min() function returns the smallest value from a set of values or the smallest item in an iterable passed as its parameter. It's useful when you need to quickly determine the minimum value from a group of numbers or objects. For example:Pythona = [23,25,65,21,98] print(min(a)) b = ["banana", 4 min read Python next() method Python's next() function returns the next item of an iterator. Example Let us see a few examples to see how the next() method in Python works. Python3 l = [1, 2, 3] l_iter = iter(l) print(next(l_iter)) Output1 Note: The .next() method was a method for iterating over a sequence in Python 2.  It has 4 min read Python oct() Function Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal 2 min read ord() function in Python Python ord() function returns the Unicode code of a given single character. It is a modern encoding standard that aims to represent every character in every language.Unicode includes:ASCII characters (first 128 code points)Emojis, currency symbols, accented characters, etc.For example, unicode of 'A 2 min read pow() Function - Python pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result. Example:Pythonprint(pow(3,2))Output9 Explanation: pow(3, 2) calculates 32 = 9, where the base is positive and t 2 min read Python print() function The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s 2 min read Python range() function The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops.ExampleIn the given example, we are printing the number from 0 to 4.Pythonfor i in range(5): print(i, end=" ") print()Output:0 1 7 min read Like