- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Test if tuple is distinct in Python
When it is required to test if a tuple has distinct elements in it, the 'set' method and the 'len' method can be used.
Python comes with a datatype known as 'set'. This 'set' contains elements that are unique only.
The 'len' method gives the length of the parameter passed to it.
Below is a demonstration of the same −
Example
my_tuple_1 = (11, 14, 54, 0, 58, 41) print("The tuple is : ") print(my_tuple_1) my_result = len(set(my_tuple_1)) == len(my_tuple_1) print("Is the tuple distinct ? ") print(my_result)
Output
The tuple is : (11, 14, 54, 0, 58, 41) Is the tuple distinct ? True
Explanation
- A tuple is defined and is displayed on the console.
- The tuple is converted to a set, and its length, and the length of the original tuple is determined.
- These two are checked using the '==' operator.
- This is assigned to a value.
- It is displayed on the console.
- Related Articles
- Test if Tuple contains K in Python
- Python – Test if tuple list has a single element
- Check if variable is tuple in Python
- Python – Test if list is Palindrome
- Check if element is present in tuple in Python
- Check if one tuple is subset of other in Python
- Python – Check if any list element is present in Tuple
- Check if element is present in tuple of tuples in Python
- Check if all array elements are distinct in Python
- Check if tuple has any None value in Python
- Check if tuple and list are identical in Python
- What is tuple unpacking in Python?
- Python – Test if Rows have Similar frequency
- Check if all sub-numbers have distinct Digit product in Python
- Flatten tuple of List to tuple in Python

Advertisements