Python - Order Tuples by List - GeeksforGeeks

10 Jul, 2020
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 comprehensionThe 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 listtest_list = [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)]  # printing original listprint("The original list is : " + str(test_list))  # initializing order list ord_list = ['Geeks', 'best', 'CS', 'Gfg']  # Order Tuples by List# Using dict() + list comprehensiontemp = dict(test_list)res = [(key, temp[key]) for key in ord_list]  # printing result print("The ordered tuple list : " + str(res)) 
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() + lambdaThe 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 listtest_list = [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)]  # printing original listprint("The original list is : " + str(test_list))  # initializing order list ord_list = ['Geeks', 'best', 'CS', 'Gfg']  # Order Tuples by List# Using setdefault() + sorted() + lambdatemp = 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)) 
The original list is : [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)]
The ordered tuple list : [('Geeks', 2), ('best', 9), ('CS', 10), ('Gfg', 3)]

Python List-of-Tuples
Python tuple-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
Python program to check whether a number is Prime or not