Python Practice › Tuple Programs › Program 36
Are all tuple elements distinct?
Write a Python program to test whether all elements of a tuple are distinct.
Input
(1, 2, 3, 4), (3, 4, 5, 4, 6, 3)
Output
(1, 2, 3, 4) is distinct (3, 4, 5, 4, 6, 3) is not distinct
Approach
- Compare
len(set(t))withlen(t).
Solution
for t in [(1, 2, 3, 4), (3, 4, 5, 4, 6, 3)]: print(t, "is distinct" if len(set(t)) == len(t) else "is not distinct")Output
(1, 2, 3, 4) is distinct (3, 4, 5, 4, 6, 3) is not distinct
Learn the concept
New to this topic? Read the lesson, then come back.