Sort a Tuple in Python

avatar

Borislav Hadzhiev

Last updated: Jun 29, 2022

banner

Photo from Unsplash

Sort a Tuple in Python #

To sort a tuple in Python:

  1. Pass the tuple to the sorted() function.
  2. The function will return a new sorted list from the items in the tuple.
  3. Pass the sorted list to the tuple() class to convert it to a tuple.
main.py
# ✅ sort a tuple containing numbers my_tuple_1 = (3, 1, 2, 8, 10) sorted_tuple_1 = tuple(sorted(my_tuple_1)) print(sorted_tuple_1) # 👉️ [1, 2, 3, 8, 10] # ------------------ # ✅ sort a tuple containing strings my_tuple_2 = ('d', 'b', 'c', 'a') sorted_tuple_2 = tuple(sorted(my_tuple_2)) print(sorted_tuple_2) # 👉️ ['a', 'b', 'c', 'd'] # ------------------ # ✅ sort a list of tuples by second element in each tuple my_list_of_tuples = [('a', 100), ('b', 50), ('c', 75)] result = sorted(my_list_of_tuples, key=lambda t: t[1]) print(result) # 👉️ [('b', 50), ('c', 75), ('a', 100)]
Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

Since tuples cannot be changed, the only way to sort a tuple is to create a new tuple with the desired order of items.

The sorted function takes an iterable and returns a new sorted list from the items in the iterable.

main.py
my_tuple_1 = (3, 1, 2, 8, 10) sorted_list = sorted(my_tuple_1) print(sorted_list) # 👉️ [1, 2, 3, 8, 10]

You can pass the list to the tuple() class to convert it back to a tuple.

The sorted() function takes an optional key argument that can be used to sort by different criteria.

main.py
my_tuple_2 = ('abc', 'abcd', 'a', 'ab',) sorted_tuple_2 = tuple( sorted(my_tuple_2, key=lambda s: len(s)) ) print(sorted_tuple_2) # 👉️ ('a', 'ab', 'abc', 'abcd')

The key argument can be set to a function that determines the sorting criteria.

The example sorts the items in the tuple by length (ascending).

The sorted() method also takes an optional reverse argument.

main.py
my_tuple_1 = (3, 1, 2, 8, 10) sorted_tuple_1 = tuple(sorted(my_tuple_1, reverse=True)) print(sorted_tuple_1) # 👉️ (10, 8, 3, 2, 1) # ------------------ my_tuple_2 = ('abc', 'abcd', 'a', 'ab',) sorted_tuple_2 = tuple( sorted(my_tuple_2, key=lambda s: len(s), reverse=True) ) print(sorted_tuple_2) # 👉️ ('abcd', 'abc', 'ab', 'a')
If the reverse argument is set to True, then the elements are sorted as if each comparison were reversed.

You can also use the key argument to sort a list of tuples.

main.py
# ✅ sort a list of tuples by second element in each tuple my_list_of_tuples = [('a', 100), ('b', 50), ('c', 75)] result = sorted(my_list_of_tuples, key=lambda t: t[1]) print(result) # 👉️ [('b', 50), ('c', 75), ('a', 100)]

We simply selected the item in each tuple that we want to sort by.

The example sorts the list of tuples by the second item.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.