Python Practice › Tuple Programs › Program 58
Empty set when nothing is symmetrical
Write a Python program that returns an empty set if no tuples in a list are symmetrical.
Input
[(1, 5, 7), (3, 4), (4, 9, 0)]
Output
set()
{(1, 2)}Approach
- Same check as before; the set stays empty when nothing matches.
Solution
def symmetrical(data): pool = set(data) return {t for t in data if t[::-1] in pool and t[::-1] != t and t < t[::-1]} print(symmetrical([(1, 5, 7), (3, 4), (4, 9, 0)]))print(symmetrical([(1, 2), (2, 1)]))Output
set()
{(1, 2)}Learn the concept
New to this topic? Read the lesson, then come back.