Python Practice › Tuple Programs › Program 58

Empty set when nothing is symmetrical

MediumTuple Programs · 58 of 70

Write a Python program that returns an empty set if no tuples in a list are symmetrical.

Advertisement
Input
[(1, 5, 7), (3, 4), (4, 9, 0)]
Output
set()
{(1, 2)}

Approach

  1. Same check as before; the set stays empty when nothing matches.

Solution

Python
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)}
Advertisement

Learn the concept

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

More tuple programs

← All 70 Tuple Programs