Sort a List, String, Tuple in Python: sort, sorted

Modified: | Tags: Python, List, String

In Python, there are two ways to sort a list in ascending or descending order: the sort() method and the built-in sorted() function. To sort a string (str) or tuple, use sorted().

For information on reversing or randomly shuffling elements, refer to the following articles.

Sort a list using the sort() method

sort() is a method for the list type.

sort() is a destructive process that sorts the original list in place.

l = [3, 1, 4, 5, 2] l.sort() print(l) # [1, 2, 3, 4, 5] 

Note that sort() returns None.

print(l.sort()) # None 

By default, the list is sorted in ascending order. To sort in descending order, set the reverse argument to True.

l.sort(reverse=True) print(l) # [5, 4, 3, 2, 1] 

Sort a list using the built-in sorted() function

sorted() is a built-in function.

sorted() returns a sorted list, while the original list remains unchanged.

l = [3, 1, 4, 5, 2] l_sorted = sorted(l) print(l_sorted) # [1, 2, 3, 4, 5] print(l) # [3, 1, 4, 5, 2] 

Like sort(), the list is sorted in ascending order by default. To sort in descending order, set the reverse argument to True.

l_reverse_sorted = sorted(l, reverse=True) print(l_reverse_sorted) # [5, 4, 3, 2, 1] print(l) # [3, 1, 4, 5, 2] 

Sort with the key argument in sort() and sorted()

Both sort() and sorted() support the key argument, which accepts a callable object (e.g., a function) that takes a single argument.

For example, you can sort by absolute value for numbers or the character count for strings. While the following examples use sorted(), the same approach applies to sort().

l = [-3, 1, 4, -5, 2] print(sorted(l)) # [-5, -3, 1, 2, 4] print(sorted(l, key=abs)) # [1, 2, -3, 4, -5] 
l = ['b', 'cc', 'aaa'] print(sorted(l)) # ['aaa', 'b', 'cc'] print(sorted(l, key=len)) # ['b', 'cc', 'aaa'] 

To learn more about key, refer to the following article.

You can find examples using key in the following articles.

How to sort a string and tuple

Since strings and tuples are immutable, they do not have a sort() method that can modify the original object in place.

However, sorted() accepts any iterable objects, including lists, strings, and tuples, and returns a new sorted list.

Sort strings

When a string is passed to sorted(), it returns a list of sorted characters.

s = 'cebad' l_sorted = sorted(s) print(l_sorted) # ['a', 'b', 'c', 'd', 'e'] print(s) # cebad 

Use the join() method to concatenate a list of characters into a single string.

s_sorted = ''.join(l_sorted) print(s_sorted) # abcde 

You can write it all at once. To sort in descending order, set the reverse argument to True.

s_sorted = ''.join(sorted(s)) print(s_sorted) # abcde s_reverse_sorted = ''.join(sorted(s, reverse=True)) print(s_reverse_sorted) # edcba 

The above examples are for sorting the string itself. If you deal with a list of strings, you can use both sort() and sorted().

l = ['banana', 'cherry', 'apple'] print(sorted(l)) # ['apple', 'banana', 'cherry'] l.sort() print(l) # ['apple', 'banana', 'cherry'] 

Character order is determined by the Unicode code points of the characters.

Sort tuples

Sorting tuples follows the same approach as with strings. sorted() returns a sorted list when a tuple is passed.

t = (3, 1, 4, 5, 2) l_sorted = sorted(t) print(l_sorted) # [1, 2, 3, 4, 5] print(t) # (3, 1, 4, 5, 2) 

Use tuple() to convert a list to a tuple.

t_sorted = tuple(l_sorted) print(t_sorted) # (1, 2, 3, 4, 5) 

You can write it all at once. To sort in descending order, set the reverse argument to True.

t_sorted = tuple(sorted(t)) print(t_sorted) # (1, 2, 3, 4, 5) t_reverse_sorted = tuple(sorted(t, reverse=True)) print(t_reverse_sorted) # (5, 4, 3, 2, 1) 

Related Categories

Related Articles