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.

Leave a Comment