In this python tuple program, we will remove empty tuples from a list of tuples.
Steps to solve the program
- Take a list of tuples as input.
- Use for loop to iterate over each tuple.
- If the length of the tuple is equal to 0 i.e. tuple is empty then remove that tuple from the list using remove().
- 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',)]