Python Practice › Tuple Programs › Program 36

Are all tuple elements distinct?

EasyTuple Programs · 36 of 70

Write a Python program to test whether all elements of a tuple are distinct.

Advertisement
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

  1. Compare len(set(t)) with len(t).

Solution

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

Learn the concept

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

More tuple programs

← All 70 Tuple Programs