In this python tuple program, we will remove Tuples from the List having every element as None.
Steps to solve the program
- Take a list of tuples as input and create an empty list.
- Use a for loop to iterate over tuples in the list.
- Use the following condition to check whether all the elements in the tuple are equal to None.
- if not all(ele == None for ele in tup.
- If not then add those tuples in the empty list using append().
- Print the output.
l = [(None,),(None,None),(5,4),(1,6,7),(None,1)]
print("Origianl list: ",l)
result = []
for tup in l:
if not all(ele == None for ele in tup):
result.append(tup)
print("After removing tuples: ",result)
Output :
Origianl list: [(None,), (None, None), (5, 4), (1, 6, 7), (None, 1)]
After removing tuples: [(5, 4), (1, 6, 7), (None, 1)]