Python Practice › Tuple Programs › Program 28

Remove tuples that are all None

EasyTuple Programs · 28 of 70

Write a Python program to remove tuples in which every element is None.

Advertisement
Input
[(None,), (None, None), (5, 4), (1, 6, 7), (None, 1)]
Output
[(5, 4), (1, 6, 7), (None, 1)]

Approach

  1. Keep tuples where not all items are None.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs