Python Practice › Tuple Programs › Program 49
Tuples with no negative elements
Write a Python program to find tuples whose elements are all zero or positive.
Input
[(1, 7), (-4, -5), (0, 6), (-1, 3)]
Output
[(1, 7), (0, 6)]
Approach
all(x >= 0 for x in t).
Solution
data = [(1, 7), (-4, -5), (0, 6), (-1, 3)]print([t for t in data if all(x >= 0 for x in t)])Output
[(1, 7), (0, 6)]
Learn the concept
New to this topic? Read the lesson, then come back.