Python tuple program to count the total number of unique tuples in a list

In this python tuple program, we will count the total number of unique tuples in a list.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Create a set of the given list to remove duplicate tuples using set().
  3. Now count the total number of unique tuples using len().
  4. Print the output.
				
					l =[(8,9),(4,7),(3,6),(8,9)]
print("Original list of tuples: ",l)
print("Total number of unique tuples: ",len(set(l)))
				
			

Output :

				
					Original list of tuples:  [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples:  3
				
			

convert a binary tuple to an integer.

calculate the average of the elements in the tuple.

Find common elements between two lists of tuples

In this python tuple program, we will find common elements between two lists of tuples.

Steps to solve the program
  1. Take two lists of tuples as input.
  2. Create a set of two lists to remove duplicate tuples by using set().
  3. Find the common tuples between two lists of tuples using the ” operator.
  4. Print the output.
				
					tup1 = [(1,5),(4,8),(3,9)]
tup2 = [(3,9),(5,6),(1,5),(0,4)]
print(f"Tuples: {tup1} and {tup2}")
result = (set(tup1) & set(tup2))
print("Common elements between tuples: ",result)
				
			

Output :

				
					Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples:  {(3, 9), (1, 5)}
				
			

order tuples by external list.

convert a binary tuple to an integer.

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.

Python tuple program to concatenate two tuples

In this python tuple program, we will concatenate two tuples.

Steps to solve the program
  1. Take two tuples as input.
  2. Concatenate the two tuples using the ” operator.
  3. Print the output.
				
					tup1 = ('s','q','a')
tup2 = ('t','o','o','l')
print(f"Original tuple: {tup1} {tup2}")
result = tup1+tup2
print("Combined tuple: ",result)
				
			

Output :

				
					Original tuple: ('s', 'q', 'a') ('t', 'o', 'o', 'l')
Combined tuple:  ('s', 'q', 'a', 't', 'o', 'o', 'l')
				
			

sort a list of tuples by the minimum value of a tuple.

order tuples by external list.

Python tuple program to sort a list if tuples by the minimum value of a tuple

In this python tuple program, we will sort a list if tuples by the minimum value of a tuple.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use sorted() to sort the tuple and pass the lambda function to sort the tuple by the minimum value to the key inside the sorted().
  3. Print the output.
				
					l = [(1,5,7),(3,4,2),(4,9,0)]
print("Original list of tuples: ",l)
l1 = sorted(l,key = lambda x: min(x))
print("After sorting: ",l1)
				
			

Output :

				
					Original list of tuples:  [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting:  [(4, 9, 0), (1, 5, 7), (3, 4, 2)]
				
			

sort a tuple by the maximum value of a tuple.

concatenate two tuples.

Python tuple program to sort a tuple by the maximum value of a tuple

In this python tuple program, we will sort a tuple by the maximum value of a tuple.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use sorted() to sort the tuple and pass the lambda function to sort the tuple by the maximum value to the key inside the sorted().
  3. Print the output.
				
					l = [ (1, 5, 7), (3, 4, 2), (4, 9, 0) ]
print("Original list of tuples: ",l)
l1 = sorted(l,key = lambda x: max(x),reverse=True)
print("After sorting: ",l1)
				
			

Output :

				
					Original list of tuples:  [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting:  [(4, 9, 0), (1, 5, 7), (3, 4, 2)]
				
			

remove nested elements from a tuple.

sort a list of tuples by the minimum value of a tuple.

Python tuple program to remove nested elements from a tuple

In this python tuple program, we will remove nested elements from a tuple.

Steps to solve the program
  1. Take a tuple as input and create an empty list.
  2. Use a for loop to iterate over values from the tuple.
  3. If the type of the value is not a tuple then add those values to the empty list.
  4. Now convert the new list to a tuple using tuple().
  5. Print the output.
				
					tup = ('s','q','a',('t','o','o','l','s'),'i','s','b','e','s','t')
print("Original tuple: ",tup)
l = []
for char in tup:
    if type(char) != tuple:
        l.append(char)
new = tuple(l)
print("New tuple: ",new)
				
			

Output :

				
					Original tuple:  ('s', 'q', 'a', ('t', 'o', 'o', 'l', 's'), 'i', 's', 'b', 'e', 's', 't')
New tuple:  ('s', 'q', 'a', 'i', 's', 'b', 'e', 's', 't')
				
			

return an empty set if no tuples are symmetrical.

sort a tuple by the maximum value of a tuple.

Program to return an empty set if no tuples are symmetrical

In this python tuple program, we will return an empty set if no tuples are symmetrical.

Steps to solve the program
				
					l = [(1, 5), (3, 4), (4, 9)]
print("Original list of tuples: ",l)
temp = set(l) & {(b, a) for a, b in l}
sym = {(a, b) for a, b in temp if a < b}
print("Tuples that are symmetric: ",sym)
				
			

Output :

				
					Original list of tuples:  [(1, 5), (3, 4), (4, 9)]
Tuples that are symmetric:  set()
				
			

extract tuples that are symmetrical with others from a list of tuples.

remove nested elements from a tuple.

Extract tuples that are symmetrical with others from a list of tuples

In this python tuple program, we will extract tuples that are symmetrical with others from a list of tuples.

Steps to solve the program
				
					l = [('a','b'),('d','e'),('b','a')]
print("Original list of tuples: ",l)
temp = set(l) & {(b, a) for a, b in l}
sym = {(a, b) for a, b in temp if a < b}
print("Tuples that are symmetric: ",sym)
				
			

Output :

				
					Original list of tuples:  [('a', 'b'), ('d', 'e'), ('b', 'a')]
Tuples that are symmetric:  {('a', 'b')}
				
			

convert a list of lists to a tuple of tuples.

return an empty set if no tuples are symmetrical.

Program to convert a list of lists to a tuple of tuples

In this python tuple program, we will convert a list of lists to a tuple of tuples.

Steps to solve the program
  1. Take a list of lists as input and create an empty list.
  2. Use a for loop to iterate over lists in the list.
  3. Add the list to the empty list using append() after converting it to a tuple using tuple().
  4. Now convert the new list to a tuple using tuple().
  5. Print the output.
				
					l = [['sqatools'],['is'],['best']]
print("List of lists: ",l)
l1 = []
for lists in l:
    l1.append(tuple(lists))
tup = tuple(l1)
print("Tuple of tuples: ",tup)
				
			

Output :

				
					List of lists:  [['sqatools'], ['is'], ['best']]
Tuple of tuples:  (('sqatools',), ('is',), ('best',))
				
			

extract tuples that are symmetrical with others from a list of tuples.