Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Sort list of tuples by specific ordering

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 24 Nov, 2022
Improve Article
Save Article

The normal sorting of tuples has been dealt previously. This article aims at sorting the given list of tuples by the second element, based on the order provided in some list. 

Method #1 : Using list comprehension + filter() + lambda Above three functions can be combined to perform the particular task in which list comprehension performs the iteration, lambda function is used as helper function for filtering to sort according to second element of tuple. 

Python3




# Python3 code to demonstrate
# sort list of tuples according to second
# using list comprehension + filter() + lambda
 
# initializing list of tuples
test_list = [('a', 2), ('c', 3), ('d', 4)]
 
# initializing sort order
sort_order = [4, 2, 3]
 
# printing the original list
print("The original list is : " + str(test_list))
 
# printing sort order list
print("The sort order list is : " + str(sort_order))
 
# using list comprehension + filter() + lambda
# sort list of tuples according to second
res = [i for j in sort_order
       for i in filter(lambda k: k[1] == j, test_list)]
 
# printing result
print("The list after appropriate sorting : " + str(res))

Output

The original list is : [('a', 2), ('c', 3), ('d', 4)]
The sort order list is : [4, 2, 3]
The list after appropriate sorting : [('d', 4), ('a', 2), ('c', 3)]

  Method #2 : Using sorted() + index() + lambda The sorted function can be used to sort according to order specified. The index function specifies that second element of tuple has to be taken into considerations and all are joined with help of lambda. 

Python3




# Python3 code to demonstrate
# sort list of tuples according to second
# using sorted() + index() + lambda
 
# initializing list of tuples
test_list = [('a', 2), ('c', 3), ('d', 4)]
 
# initializing sort order
sort_order = [4, 2, 3]
 
# printing the original list
print("The original list is : " + str(test_list))
 
# printing sort order list
print("The sort order list is : " + str(sort_order))
 
# using sorted() + index() + lambda
# sort list of tuples according to second
res = list(sorted(test_list,
                  key=lambda i: sort_order.index(i[1])))
 
# printing result
print("The list after appropriate sorting : " + str(res))

Output

The original list is : [('a', 2), ('c', 3), ('d', 4)]
The sort order list is : [4, 2, 3]
The list after appropriate sorting : [('d', 4), ('a', 2), ('c', 3)]

Method #3 : Using for loop

Python3




# Python3 code to demonstrate
# sort list of tuples according to second
 
# initializing list of tuples
test_list = [('a', 2), ('c', 3), ('d', 4)]
 
# initializing sort order
sort_order = [4, 2, 3]
 
# printing the original list
print("The original list is : " + str(test_list))
 
# printing sort order list
print("The sort order list is : " + str(sort_order))
res = []
for i in sort_order:
    for j in test_list:
        if(j[1] == i):
            res.append(j)
 
# printing result
print("The list after appropriate sorting : " + str(res))

Output

The original list is : [('a', 2), ('c', 3), ('d', 4)]
The sort order list is : [4, 2, 3]
The list after appropriate sorting : [('d', 4), ('a', 2), ('c', 3)]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!