89DEVs

Python sorted() function

sorted() overview

The Python sorted() function sorts an iterable in a specific order, either ascending or descending and returns it as a sorted list. The difference to the sort method of lists is that sort() doesn't return anything and changes the original list instead.

sorted() lists

To sort a list, use the sorted() function or the list.sort() method. The difference is that sorted() returns a new list. # sorted() lists myList = [1, 5, 9, 4] Sorted = sorted(myList) Reversed = sorted(myList, reverse=True) print(Sorted) print(Reversed) The sorted and reversed lists are returned. [1, 4, 5, 9] [9, 5, 4, 1]

sorted() strings

If a string is passed to the sorted() function a list sorted in alphabetical order is returned. The order can be reversed by using the reverse flag. # sorted() strings myString = 'Python' SortedString = sorted(myString) ReversedString = sorted(myString, reverse=True) print(SortedString) print(ReversedString) The sorted and reversed string are returned. ['P', 'h', 'n', 'o', 't', 'y'] ['y', 't', 'o', 'n', 'h', 'P']

sorted() using key

To modify the behaviour of the sorted() function, it's possible to pass in a key as optional argument. For example we can pass in the len() function as key argument to sort the list based on the length of each element. # sorted() using key function myList = ['abc', 'a', 'ab', 'abcd'] # sort list by length of element Sorted = sorted(myList, key=len) print(Sorted) The list is sorted by the length of each element. The key argument can be combined with the reverse argument to show to longest element first. ['a', 'ab', 'abc', 'abcd']

sorted() on nested objects

If a nested object like a nested tuple is passed to the sorted() function, it sorts the elements based on the first key. # SORTED() ON NESTED OBJECTS myNestedTuple = [('Mike', 12, 18), ('Dave', 4, 25), ('Anna', 22, 8)] Sorted = sorted(myNestedTuple) print(Sorted) The elements are sorted in order of the first key, in this case, the name. To modify this behavior use the key argument. [('Anna', 22, 8), ('Dave', 4, 25), ('Mike', 12, 18)]

sorted() syntax

The syntax of the sorted() function is: sorted(iterable[, key=None, reverse=False])

sorted() arguments

The sorted() function accepts between 1 and 3 arguments:
argument required description
iterable required the iterable to be sorted (string, tuple, list, set, dictionary, ...)
reverse optional If True, sorted list is reversed. Default: False
key optional function to be used as key for the sorting. Default None
If less than one or more than three arguments are passed, a TypeError exception is raised.

sorted() return values

The sorted() function returns a sorted list. Even when another object type is passed to the sorted() function, the return value will always be a list.

related functions

The sorted() function is related to the sort() method of lists. While list.sort() doesn't return anything, the sorted() function returns a new list and doesn't change the passed iterable.

                
        

Summary


Click to jump to section