Sorting any sequence is very easy in Python using built-in method sorted() which does all the hard work for you.
Sorted() sorts any sequence (list, tuple) and always returns a list with the elements in sorted manner, without modifying the original sequence.
Syntax : sorted(iterable, key, reverse)
Parameters : sorted takes three parameters from which two are optional.
- Iterable : sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
- Key(optional) : A function that would server as a key or a basis of sort comparison.
- Reverse(optional) : If set true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.
Example 1
x = [2, 8, 1, 4, 6, 3, 7] print ("Sorted List returned :"), print (sorted(x)) print ("\nReverse sort :"), print (sorted(x, reverse = True)) print ("\nOriginal list not modified :"), print (x) |
Output : Sorted List returned : [1, 2, 3, 4, 6, 7, 8] Reverse sort : [8, 7, 6, 4, 3, 2, 1] Original list not modified : [2, 8, 1, 4, 6, 3, 7]
Example 2 : Sorting different data types
# List x = ['q', 'w', 'r', 'e', 't', 'y'] print (sorted(x)) # Tuple x = ('q', 'w', 'e', 'r', 't', 'y') print (sorted(x)) # String-sorted based on ASCII translations x = "python"print (sorted(x)) # Dictionary x = {'q':1, 'w':2, 'e':3, 'r':4, 't':5, 'y':6} print (sorted(x)) # Set x = {'q', 'w', 'e', 'r', 't', 'y'} print (sorted(x)) # Frozen Set x = frozenset(('q', 'w', 'e', 'r', 't', 'y')) print (sorted(x)) |
Output : ['e', 'q', 'r', 't', 'w', 'y'] ['e', 'q', 'r', 't', 'w', 'y'] ['h', 'n', 'o', 'p', 't', 'y'] ['e', 'q', 'r', 't', 'w', 'y'] ['e', 'q', 'r', 't', 'w', 'y'] ['e', 'q', 'r', 't', 'w', 'y']
Custom Sorting using the key parameter
sorted() function has an optional parameter called ‘key’ which takes a function as its value. This key function transforms each element before sorting, it takes the value and returns 1 value which is then used within sort instead of the original value.
For example, if we pass a list of strings in sorted(), it gets sorted alphabetically . But if we specify key = len, i.e. give len function as key, then the strings would be passed to len, and the value it returns, i.e. the length of strings will be sorted. Which means that the strings would be sorted based on their lengths instead
L = ["cccc", "b", "dd", "aaa"] print ("Normal sort :", sorted(L)) print ("Sort with len :", sorted(L, key = len)) |
Output : Normal sort : ['aaa', 'b', 'cccc', 'dd'] Sort with len : ['b', 'dd', 'aaa', 'cccc']
Key also takes user-defined functions as its value for the basis of sorting.
# Sort a list of integers based on # their remainder on dividing from 7 def func(x): return x % 7 L = [15, 3, 11, 7] print ("Normal sort :", sorted(L)) print ("Sorted with key:", sorted(L, key = func)) |
Output : Normal sort : [3, 7, 11, 15] Sorted with key: [7, 15, 3, 11]
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- a.sort(), sorted(a), np.argsort(a) and np.lexsort(b, a) in Python
- Merge two sorted arrays in Python using heapq
- Creating a sorted merged list of two unsorted lists in Python
- Python sorted() to check if two strings are anagram or not
- Python | Find common elements in three sorted arrays by dictionary intersection
- Python | Find missing numbers in a sorted list range
- Python | Check if list is sorted or not
- Python program to insert an element into sorted list
- Python | Combining two sorted lists
- Python | Inserting item in sorted list maintaining order
- Python | Convert list of string into sorted list of integer
- Python | Get items in sorted order from given dictionary
- Python | Indices of sorted list of list elements
- Python sorted containers | An Introduction
- Python | Returning index of a sorted list
- Python | Relative sorted order in Matrix
- Python - Difference between sorted() and sort()
- Python - Get a sorted list of random integers with unique elements
- Python | Check for Descending Sorted List

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
