Skip to content
Related Articles

Related Articles

Python – Order Tuples by List

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 08 Aug, 2022
Improve Article
Save Article

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




# 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




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

Method #3: Using lists and index() method

Python3




# Python3 code to demonstrate working of
# Order Tuples by List
 
# 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']
 
res=[]
x=[]
for i in test_list:
    x.append(i[0])
for i in ord_list:
    if i in x:
        res.append(test_list[x.index(i)])
# 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)]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!