Home »
Python »
Python Programs
Python program to order tuples by list
Here, we have a list of tuples and a sorting list. We need to order the tuples of the tuple list using the sorting list provided in Python programming language.
By Shivang Yadav Last updated : December 12, 2023
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
Example:
tuple = ("python", "includehelp", 43, 54.23)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets [].
Order Tuples by List
We are given a list of tuples and a sorting list. We will create a python program for sorting list of tuples based on the values of the sorting list.
Example
Input:
tupList = [('l', 5), ('k', 2), ('a', 1), ('e', 6)], sortList = ['l', 'a', 'k', 'e']
Output:
[('l', 5), ('a', 1), ('k', 2), ('e', 6)]
We need to sort the element of the list of tuples in python based on the provided sortList. Python provides multiple ways to perform the task. Some of them are :
Method 1: By creating an empty dictionary & storing all list elements
A method to sort the list of tuples based on a list is by creating an empty dictionary and storing all list elements as keys with empty value. Then feed values to the dictionary.
This is done using a combination of setdefault() + sorted() + lambda function.
Program
# Python program to order tuples by list
# Creating and printing tuple list and list
tupList = [('l', 5), ('k', 2), ('a', 1), ('e', 6)]
print("The original list is : " + str(tupList))
sortingList = ['l', 'a', 'k', 'e']
print("The sorting list is " + str(sortingList))
# Sorting tuple list based on Sorting list
valDict = dict()
for key, ele in enumerate(sortingList):
valDict.setdefault(ele, []).append(key)
sortedList = sorted(tupList, key = lambda ele: valDict[ele[0]].pop())
# Printing sorted List
print("The ordered tuple list based on tuple list: " + str(sortedList))
Output:
The original list is : [('l', 5), ('k', 2), ('a', 1), ('e', 6)]
The sorting list is ['l', 'a', 'k', 'e']
The ordered tuple list based on tuple list: [('l', 5), ('a', 1), ('k', 2), ('e', 6)]
Method 2
Another method to sort the list of tuples is by creating a dictionary with values of the list of tuples. Then, we will use list comprehension to store the values of the dictionary sorted by using sorting list.
Program
# Python program to order list of tuples by sorted list
# creating and printing initial lists
tupList = [('l', 5), ('k', 2), ('a', 1), ('e', 6)]
print("The original list is : " + str(tupList))
sortingList = ['l', 'a', 'k', 'e']
print("The sorting list is " + str(sortingList))
# Sorting list of tuple
Dic = dict(tupList)
sortedTupList = [(key, Dic[key]) for key in sortingList]
# Printing sorted List
print("The ordered tuple list : " + str(sortedTupList))
Output:
The original list is : [('l', 5), ('k', 2), ('a', 1), ('e', 6)]
The sorting list is ['l', 'a', 'k', 'e']
The ordered tuple list : [('l', 5), ('a', 1), ('k', 2), ('e', 6)]
Python Tuple Programs »