Open In App
Related Articles

Python | Custom sorting in list of tuples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Sometimes, while working with list of tuples, we can have a problem in which we need to perform it’s sorting. Naive sorting is easier, but sometimes, we have to perform custom sorting, i.e by decreasing order of first element and increasing order of 2nd element. And these can also be in cases of different types of tuples. Let’s discuss certain cases and solutions to perform this kind of custom sorting. 

Method #1 : Using sorted() + lambda This task can be performed using the combination of above functions. In this, we just perform the normal sort, but in addition we feed a lambda function which handles the case of custom sorting discussed above. 

Python3

# Python3 code to demonstrate working of
# Custom sorting in list of tuples
# Using sorted() + lambda
 
# Initializing list
test_list = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Custom sorting in list of tuples
# Using sorted() + lambda
res = sorted(test_list, key = lambda sub: (-sub[0], sub[1]))
 
# printing result
print("The tuple after custom sorting is : " + str(res))

                    
Output : 
The original list is : [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)]
The tuple after custom sorting is : [(10, 1), (10, 4), (7, 5), (7, 8), (5, 6)]

Time Complexity: O(n*nlogn), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using sorted() + lambda() + sum() ( With sum of tuple condition) In this method, similar solution sustains. But the case here is that we have tuple as the 2nd element of tuple and its sum has to considered for sort order. Other functions than summation can be extended in similar solution. 

Python3

# Python3 code to demonstrate working of
# Custom sorting in list of tuples
# Using sorted() + lambda() + sum()
 
# Initializing list
test_list = [(7, (8, 4)), (5, (6, 1)), (7, (5, 3)), (10, (5, 4)), (10, (1, 3))]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Custom sorting in list of tuples
# Using sorted() + lambda() + sum()
res = sorted(test_list, key = lambda sub: (-sub[0], sum(sub[1])))
 
# printing result
print("The tuple after custom sorting is : " + str(res))

                    
Output : 
The original list is : [(7, (8, 4)), (5, (6, 1)), (7, (5, 3)), (10, (5, 4)), (10, (1, 3))]
The tuple after custom sorting is : [(10, (1, 3)), (10, (5, 4)), (7, (5, 3)), (7, (8, 4)), (5, (6, 1))]

Using Bubble sort:

Approach:

  • Initialize the list to be sorted.
  • Find the length of the list.
  • Loop through each element of the list.
  • Within the outer loop, loop through each element of the list from 0 to n-i-1.
  • Compare the first elements of the current and next tuples. If the first element of the next tuple is greater than the first element of the current tuple, swap the positions of the current and next tuples.
  • If the first elements of the current and next tuples are equal, compare the second elements of the current and next tuples. If the second element of the current tuple is greater than the second element of the next tuple, swap the positions of the current and next tuples.
  • After all iterations, the list will be sorted in descending order based on the first element of the tuples, and in ascending order based on the second element of the tuples.

Python3

lst = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)]
n = len(lst)
 
for i in range(n):
    for j in range(n-i-1):
        if lst[j][0] < lst[j+1][0] or (lst[j][0] == lst[j+1][0] and lst[j][1] > lst[j+1][1]):
            lst[j], lst[j+1] = lst[j+1], lst[j]
 
print(lst)

                    

Output
[(10, 1), (10, 4), (7, 5), (7, 8), (5, 6)]

Time complexity: O(n^2)
Auxiliary Space: O(1)



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads