Python tuple program to remove empty tuples from a list of tuples

In this python tuple program, we will remove empty tuples from a list of tuples.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use for loop to iterate over each tuple.
  3. If the length of the tuple is equal to 0 i.e. tuple is empty then remove that tuple from the list using remove().
  4. Print the output.
				
					l = [(" ",),('a','b'),('a','b','c'),('d',),()]
print("Original list of tuples: ",l)
for tup in l:
    if len(tup) == 0:
        l.remove(tup)
print("After removing empty tuples: ",l)
        
				
			

Output :

				
					Original list of tuples:  [(' ',), ('a', 'b'), ('a', 'b', 'c'), ('d',), ()]
After removing empty tuples:  [(' ',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
				
			

convert a tuple to string datatype.

sort a tuple by its float element.

Leave a Comment