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.
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.
The original tuple is : (1, 4, 5, 6) Is tuple distinct ? : True
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Test if key exists in tuple keys dictionary
- Python - Test if Tuple contains K
- Python - Test Similar Data Type in Tuple
- Python - Test if greater than preceding element in Tuple List
- Python | Count the elements in a list until an element is a Tuple
- Python | Count occurrences of an element in a Tuple
- Python | Sort a tuple by its float element
- Python | Reversing a Tuple
- Python | Pair and combine nested list to tuple list
- Python | Convert String to tuple list
- Python - Tuple key detection from value list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Join tuple elements in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.