Python Practice › Tuple Programs › Program 28
Remove tuples that are all None
Write a Python program to remove tuples in which every element is None.
Input
[(None,), (None, None), (5, 4), (1, 6, 7), (None, 1)]
Output
[(5, 4), (1, 6, 7), (None, 1)]
Approach
- Keep tuples where not all items are None.
Solution
data = [(None,), (None, None), (5, 4), (1, 6, 7), (None, 1)]print([t for t in data if not all(x is None for x in t)])Output
[(5, 4), (1, 6, 7), (None, 1)]
Line by line
Line 2
(None, 1) stays, because only one of its items is None.
Learn the concept
New to this topic? Read the lesson, then come back.