Skip to content
Related Articles

Related Articles

Improve Article

Python – Order Tuples by List

  • Difficulty Level : Medium
  • Last Updated : 10 Jul, 2020
Geek Week

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.




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




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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :