Python sorted()

The sorted() method sorts the elements of the given iterable in ascending order and returns it.


numbers = [4, 2, 12, 8]

# sort the list in ascending order sorted_numbers = sorted(numbers)
print(sorted_numbers) # Output: [2, 4, 8, 12]

Here, we created a new list to store the sorted list. This is because sorted() doesn't sort the given iterable, it creates a new list with sorted elements.

sorted() Syntax


Here, the iterable can be any sequence we want to sort, such as list, tuple, string, dictionary, set, frozen set, etc.

sorted() Parameters

By default, sorted() takes only a single parameter - iterable.

sorted() Method with Optional Parameters

Along with the iterable, the sorted() method also has two optional parameters: reverse and key.

sorted(iterable, key = …, reverse = …)


  • key - function that determines the basis for sort comparison. Default value - None
  • reverse - boolean that decides the sorting order. If True, the list is sorted in descending order

sorted() Return Value

The method returns a sorted list.

Example: Sort a List in Ascending Order

# list of vowels in random order
vowels_list = ['e', 'a', 'u', 'o', 'i']

# sort the vowels print('Sorted Vowels:', sorted(vowels_list))
# Output: Sorted Vowels: ['a', 'e', 'i', 'o', 'u']
Different Iterables for Sorting

Sorting a list

numbers = [3,2,7,5,9,11]

# Output: [2, 3, 5, 7, 9, 11]

Sorting a tuple

numbers = (4,2,1,6,8,15)

# Output: [1, 2, 4, 6, 8, 15]

Sorting a dictionary

dict = {1: 'one', 2: 'two', 3: 'three'}

# Output: [1, 2, 3]

Sorting a set

my_set = {'u', 'i', 'o', 'a', 'e'}

# Output: ['a', 'e', 'i', 'o', 'u']

Sort in Descending Order

We can use an optional reverse parameter to sort the list in descending order. For example,

numbers = [2, 7, 3, 9, 13, 11]

# sorting the numbers in descending order sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers) # Output: [13, 11, 9, 7, 3, 2]

Here, sorted(iterable, reverse = True) sorts the list in descending order.

Sorting With the Key Function

The sorted() method accepts another optional parameter- the key function. For example,

sorted(iterable, key = len)

Here, len is Python's built-in function that counts the length of an element.

In this case, the sorted() method sorts the list based on the length of the element. For example,

fruits = ['apple', 'banana', 'kiwi', 'pomegranate']

# sorts the list based on the length of each string sorted_fruits = sorted(fruits, key=len)
print('Sorted list:', sorted_fruits) # Output: Sorted list: ['kiwi', 'apple', 'banana', 'pomegranate']

Note: The list is sorted based on the length of the element, from the lowest count to the highest.

More on Python sorted()

sorted() Method vs sort() Method

The sorted() method takes elements from the original list and returns a new list with sorted elements. The original list remains unchanged. For example,

numbers = [3, 1, 4, 1, 5, 9, 2, 5, 3]

# sort using sorted() method sorted_numbers = sorted(numbers)
print("Using sorted():", sorted_numbers) print("Original list:", numbers)


Using sorted(): [1, 1, 2, 3, 3, 4, 5, 5, 9]
Original list: [3, 1, 4, 1, 5, 9, 2, 5, 3]

The sort() method modifies the original list directly and does not return any values. For example,

numbers = [3, 1, 4, 1, 5, 9, 2, 5, 3]

# sort using sort() method numbers.sort()
print("Using sort():", numbers) # Output: Using sort(): [1, 1, 2, 3, 3, 4, 5, 5, 9]

To learn more, visit Python sort().

Sorting List Using Custom Key Function

We can also use user-defined functions as the key function. For example,

# take age from each element
def get_age(person):
    return person[1]

# list of tuple
personal_info = [("Alice", 25), ("Bob", 30), ("Charlie", 22)]

# sort the list based on age sorted_personal_info= sorted(personal_info, key=get_age)
print(sorted_personal_info) # Output: [('Charlie', 22), ('Alice', 25), ('Bob', 30)]

In the above example, the personal_info is a list of tuples where the get_age function is defined as a key function.

Here, the list is sorted based on the age (i.e., second element) of each tuple.

Sort Example With Both Parameters

We can use both key and reverse parameters for sorting the list. For example,

words = ['python', 'is', 'awesome', 'and', 'fun']

# sort list on the basis of length in descending order sorted_words= sorted(words, key=len, reverse=True)
print(sorted_words) # Output: ['awesome', 'python', 'and', 'fun', 'is']

Also Read:


Write a function to check if a given list of numbers is sorted in ascending order.

  • For example, for input [1, 2, 3, 4], the outout should be True.
Did you find this article helpful?