Python tuple program to order tuples by external list

In this python tuple program, we will order tuples by external list.

Steps to solve the program
  1. Take a list of tuples and a list by which we have to order tuples as input.
  2. Create a dictionary of a list of tuples.
  3. It will set words in the tuples as keys and numbers as their values.
  4. Use a for loop to iterate over words in the list by which we have to order the tuples.
  5. Now add the word and its value in the dictionary to another list using list comprehension i.e. loop inside the list.
  6. Print the output.
				
					list1 = [('very',8),('i',6),('am',5),('happy',0)]
list2 = ['i','am','very','happy']
print("List of tuple: ",list1)
print("List: ",list2)
d = dict(list1)
result = [(key, d[key]) for key in list2]
print("Output list: ",result)
				
			

Output :

				
					List of tuple:  [('very', 8), ('i', 6), ('am', 5), ('happy', 0)]
List:  ['i', 'am', 'very', 'happy']
Output list:  [('i', 6), ('am', 5), ('very', 8), ('happy', 0)]
				
			

concatenate two tuples.

find common elements between two lists of tuples.

Leave a Comment