Python | Test if tuple is distinct
Last Updated :
23 Apr, 2023
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
# 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))
OutputThe 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
# 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))
OutputThe original tuple is : (1, 4, 5, 6)
Is tuple distinct ? : True
Method #3: Using collections.Counter()
This method uses collections.Counter() to return a dictionary of elements and their respective counts. If the maximum count of any element is 1, the tuple is distinct.
Python3
# Python3 code to demonstrate working of
# Test if tuple is distinct
# Using collections.Counter()
# importing collections for Counter
import collections
# 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 collections.Counter()
res = max(collections.Counter(test_tup).values()) == 1
# printing result
print("Is tuple distinct ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original tuple is : (1, 4, 5, 6, 1, 4)
Is tuple distinct ? : False
Time Complexity: O(n)
Auxiliary space: O(n)
Method 4 : use a boolean flag and iterate over the tuple to check if there are any duplicate elements.
step-by-step approach for the above program:
- Initialize a tuple called "test_tup" with some values.
- Initialize a boolean variable called "distinct" with the value True. This variable will be used to determine if the tuple is distinct or not.
- Use a for loop to iterate over the elements of the tuple.
- For each element in the tuple, check if it appears again in the rest of the tuple (i.e., in the slice test_tup[i+1:]).
- If the element is found in the rest of the tuple, set the "distinct" variable to False and break out of the loop, since we have found a duplicate element.
- Print the result of whether the tuple is distinct or not.
Python3
# initialize tuple
test_tup = (1, 4, 5, 6, 1, 4)
# Test if tuple is distinct
# Using boolean flag
distinct = True
for i in range(len(test_tup)):
if test_tup[i] in test_tup[i+1:]:
distinct = False
break
# printing result
print("Is tuple distinct? : " + str(distinct))
OutputIs tuple distinct? : False
Time Complexity- O(n^2)
Auxiliary space - O(1)
Similar Reads
Python - Test if Tuple contains K Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains K. This kind of problem is common in data preprocessing steps. Letâs discuss certain ways in which this task can be performed. Method #1: Using any() + map() + lambda Combination
6 min read
Python | Test if key exists in tuple keys dictionary Sometimes, while working with dictionary data, we need to check if a particular key is present in the dictionary. If keys are elementary, the solution to a problem in discussed and easier to solve. But sometimes, we can have a tuple as key of the dictionary. Let's discuss certain ways in which this
7 min read
Python - Extract tuple supersets from List Sometimes, while working with Python tuples, we can have a problem in which we need to extract all the tuples, which have all the elements in target tuple. This problem can have application in domains such as web development. Let's discuss certain way in which this problem can be solved. Input : tes
5 min read
Python - Test if tuple list has Single element Given a Tuple list, check if it is composed of only one element, used multiple times. Input : test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Output : True Explanation : All elements are equal to 3. Input : test_list = [(3, 3, 3), (3, 3), (3, 4, 3), (3, 3)] Output : False Explanation : All elemen
7 min read
How to Take List of Tuples as Input in Python? Lists of tuples are useful data structures in Python and commonly used when you need to group related elements together while maintaining immutability within each group. We may need to take input for a list of tuples from the user. This article will explore different methods to take a list of tuples
3 min read
How to Check if Tuple is empty in Python ? A Tuple is an immutable sequence, often used for grouping data. You need to check if a tuple is empty before performing operations. Checking if a tuple is empty is straightforward and can be done in multiple ways. Using the built-in len() will return the number of elements in a tuple and if the tupl
2 min read
Python | Get duplicate tuples from list Sometimes, while working with records, we can have a problem of extracting those records which occur more than once. This kind of application can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() + count() In
7 min read
Python | Replace duplicates in tuple Sometimes, while working with Python tuples, we can have a problem in which we need to remove tuples elements that occur more than one times and replace duplicas with some custom value. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + list comprehension The c
5 min read
Python | Check if tuple and list are identical Sometimes while working with different data in Python, we can have a problem of having data in different containers. In this situations, there can be need to test if data is identical cross containers. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is th
7 min read
Python - Test Similar Data Type in Tuple Sometimes, while working with Python tuples, we can have a problem in which we need to test if all the elements in the tuple have the same data type. This kind of problem can have applications across all domains such as web development and day-day programming. Let's discuss certain ways in which thi
6 min read