How to sort tuples in python?

Sorting refers to arranging the data in a particular order. A tuple is a collection of data items separated by commas and enclosed within parentheses. The items in the tuple are ordered and immutable. For example, tup_1 = (3, 9, 2, 7) represents a tuple. The tuple tup_1 when sorted results in (2, 3, 7, 9).

This article discusses the different methods to sort the tuples.

Sorting the tuple using the sort() function

We know that a tuple is immutable(unchangeable) and the sort() function modifies the original list. Therefore, to sort a tuple we convert the tuple to a list using the list() constructor. The list is sorted using the sort() function. The sorted list is converted back again to a tuple using the tuple() function.

The sort() function sorts the list in ascending order and can only be used with a list. The sort() function doesn't return anything. The two optional parameters for the sort() function are key and reverse.

The syntax for the sort() function is as follows.

#syntax:
list.sort(key = ..., reverse =...)
  • The parameter reverse takes a boolean value. If the reverse is set to True it sorts the list in descending order.
  • The parameter key takes a function as a value for user-defined comparison.

Example,

tuple_1 = (7, 6, 3, 9)
print("tuple_1: ", tuple_1)
list_1 = list(tuple_1)
print("list_1: ", list_1)
list_1.sort()
print("Sorted Tuple: ", tuple(list_1))

The above code returns the output as

tuple_1:  (7, 6, 3, 9)
list_1:  [7, 6, 3, 9]
Sorted Tuple:  (3, 6, 7, 9)

To sort the tuple in descending order we set the reverse parameter in the sort() function to True.

tuple_1 = (7, 6, 3, 9)
print("tuple_1: ", tuple_1)
list_1 = list(tuple_1)
print("list_1: ", list_1)
list_1.sort(reverse = True)
print("Sorted Tuple in descending order: ", tuple(list_1))

Output

tuple_1:  (7, 6, 3, 9)
list_1:  [7, 6, 3, 9]
Sorted Tuple in descending order:  (9, 7, 6, 3)

Sorting the tuple using the sorted() function

sorted() function is a built-in function in python that takes three arguments sequence(list, tuple, set,etc.) key(optional), reverse(optional) and returns a sorted list. The major difference between the sort() and sorted() function is, sorted() function returns a sorted list and doesn't modify the original sequence.

The syntax for the sorted() function is as follows.

#syntax:
sorted(seq, key=..., reverse=...)
  • The parameter reverse takes a boolean value. If the reverse is set to True it sorts the sequence in descending order.
  • The parameter key takes a function as a value for user-defined comparison.

A sorted() function returns a list. To get a sorted tuple, we typecast the list to a tuple using the tuple() function.

tuple_1 = (7, 6, 3, 9)
print("tuple_1: ", tuple_1)
sort_tup = tuple(sorted(tuple_1))
print("Sorted Tuple: ", sort_tup)

Output

tuple_1:  (7, 6, 3, 9)
Sorted Tuple:  (3, 6, 7, 9)

Sorting a tuple using the for loop

A tuple is immutable. Therefore, to sort a tuple we convert the tuple to a list using the list() function. Two for loops are used to repeatedly swap the elements in the list in each pass if they are not in ascending order. The sorted list is converted back to a tuple using the tuple() function.

tuple_1 = (3, 5, 8, 4.5)
print("tuple_1: ", tuple_1)
list_1 = list(tuple_1)
print("list_1 = ", list_1)
for i in range(len(list_1)):
    for j in range(i+1, len(list_1)):
        if list_1[i] > list_1[j]:
            list_1[i] , list_1[j] = list_1[j] , list_1[i]
print("Sorted Tuple: ", tuple(list_1))

The above code returns the output as

tuple_1:  (3, 5, 8, 4.5)
list_1 =  [3, 5, 8, 4.5]
Sorted Tuple:  (3, 4.5, 5, 8)
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions