Python | Sort list of tuples by specific ordering

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.