Remove Tuples from the List having every element as None

In this python tuple program, we will remove Tuples from the List having every element as None.

Steps to solve the program
  1. Take a list of tuples as input and create an empty list.
  2. Use a for loop to iterate over tuples in the list.
  3. Use the following condition to check whether all the elements in the tuple are equal to None.
  4. if not all(ele == None for ele in tup.
  5. If not then add those tuples in the empty list using append().
  6. 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)]
				
			

remove tuples from the List having an element as None.

sort a list of tuples by the first item.

Leave a Comment