Python – Order Tuples by List

Sometimes, while working with Python tuples, we can have a problem in which we need to perform ordering of all the tuples keys using external list. This problem can have application in data domains such as Data Science. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(‘Gfg’, 10), (‘best’, 3), (‘CS’, 8), (‘Geeks’, 7)], ord_list = [‘Geeks’, ‘best’, ‘CS’, ‘Gfg’]
Output : [(‘Geeks’, 7), (‘best’, 3), (‘CS’, 8), (‘Gfg’, 10)]

Input : test_list = [(‘best’, 3), (‘CS’, 8), (‘Geeks’, 7)], ord_list = [‘Geeks’, ‘best’, ‘CS’]
Output : [(‘Geeks’, 7), (‘best’, 3), (‘CS’, 8)]

Method #1 : Using dict() + list comprehension
The combination of above functions can be used to solve this problem. In this, we perform this task by converting tuple list to dictionaries, and as a second step use list comprehension to iterate through list and map the dictionary keys with values.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Order Tuples by List
# Using dict() + list comprehension
  
# initializing list
test_list = [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing order list 
ord_list = ['Geeks', 'best', 'CS', 'Gfg']
  
# Order Tuples by List
# Using dict() + list comprehension
temp = dict(test_list)
res = [(key, temp[key]) for key in ord_list]
  
# printing result 
print("The ordered tuple list : " + str(res)) 

chevron_right


Output :



The original list is : [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)]
The ordered tuple list : [('Geeks', 2), ('best', 9), ('CS', 10), ('Gfg', 3)]

 

Method #2 : Using setdefault() + sorted() + lambda
The combination of above functions can be used to solve this problem. In this, we perform task of mapping elements to indices and creating a lookup using setdefault. And, as a second step, using sorted to sort list using lookup dictionary value list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Order Tuples by List
# Using setdefault() + sorted() + lambda
  
# initializing list
test_list = [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing order list 
ord_list = ['Geeks', 'best', 'CS', 'Gfg']
  
# Order Tuples by List
# Using setdefault() + sorted() + lambda
temp = dict()
for key, ele in enumerate(ord_list):
    temp.setdefault(ele, []).append(key)       
res = sorted(test_list, key = lambda ele: temp[ele[0]].pop())  
  
# printing result 
print("The ordered tuple list : " + str(res)) 

chevron_right


Output :

The original list is : [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)]
The ordered tuple list : [('Geeks', 2), ('best', 9), ('CS', 10), ('Gfg', 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.