# Python | Test if tuple is distinct

Sometimes, while working with records, we have a problem in which we need to find if all elements of tuple are different. This can have applications in many domains including web development. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop
This is a brute force way in which this task can be performed. In this, we just iterate through all tuple elements and put it in set if it’s the first occurrence. During the subsequence occurrence we check in set, if it exists, we return False.

 `# Python3 code to demonstrate working of ` `# Test if tuple is distinct ` `# Using loop ` ` `  `# initialize tuple  ` `test_tup ``=` `(``1``, ``4``, ``5``, ``6``, ``1``, ``4``) ` ` `  `# printing original tuple  ` `print``(``"The original tuple is : "` `+` `str``(test_tup)) ` ` `  `# Test if tuple is distinct ` `# Using loop ` `res ``=` `True`  `temp ``=` `set``() ` `for` `ele ``in` `test_tup: ` `    ``if` `ele ``in` `temp: ` `        ``res ``=` `False`  `        ``break` `    ``temp.add(ele) ` ` `  `# printing result ` `print``(``"Is tuple distinct ? : "` `+` `str``(res)) `

Output :

```The original tuple is : (1, 4, 5, 6, 1, 4)
Is tuple distinct ? : False
```

Method #2 : Using `set() + len()`
In this method, we convert the tuple into a set using set(), and then check it with original tuple length, if matches, means that it was a distinct tuple and returns True.

 `# Python3 code to demonstrate working of ` `# Test if tuple is distinct ` `# Using set() + len() ` ` `  `# initialize tuple  ` `test_tup ``=` `(``1``, ``4``, ``5``, ``6``) ` ` `  `# printing original tuple  ` `print``(``"The original tuple is : "` `+` `str``(test_tup)) ` ` `  `# Test if tuple is distinct ` `# Using set() + len() ` `res ``=` `len``(``set``(test_tup)) ``=``=` `len``(test_tup) ` ` `  `# printing result ` `print``(``"Is tuple distinct ? : "` `+` `str``(res)) `

Output :

```The original tuple is : (1, 4, 5, 6)
Is tuple distinct ? : True
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.