Given a Tuple list, filter tuples that don’t contain duplicates.
Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4, 9), (2, 3, 2)]
Output : [(3, 5, 6, 7)]
Explanation : Rest all tuples have duplicate values.
Input : test_list = [(3, 5, 6, 7, 7), (3, 2, 4, 3), (9, 4, 9), (2, 3, 2)]
Output : []
Explanation : All tuples have duplicate values.
Method #1 : Using loop + set()
In this, all the tuples are iterated, and duplicacy test is done using set(), if the length of the set is the same as the tuple, it doesn’t contain a duplicate.
Python3
test_list = [( 3 , 5 , 6 , 7 ), ( 3 , 2 , 4 , 3 ), ( 9 , 4 ), ( 2 , 3 , 2 )]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
if len ( set (sub)) = = len (sub):
res.append(sub)
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
Filtered tuples : [(3, 5, 6, 7), (9, 4)]
Method #2: Using list comprehension
This performs a similar task as above, the difference being that this is one-liner and compact.
Python3
test_list = [( 3 , 5 , 6 , 7 ), ( 3 , 2 , 4 , 3 ), ( 9 , 4 ), ( 2 , 3 , 2 )]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if len ( set (sub)) = = len (sub)]
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
Filtered tuples : [(3, 5, 6, 7), (9, 4)]
Method #3: Without using any builtin methods
Python3
test_list = [( 3 , 5 , 6 , 7 ), ( 3 , 2 , 4 , 3 ), ( 9 , 4 ), ( 2 , 3 , 2 )]
print ( "The original list is : " + str (test_list))
res = []
def checkUnique(lis):
x = []
for i in lis:
if i not in x:
x.append(i)
x = tuple (x)
if (x = = lis):
return True
return False
for sub in test_list:
if (checkUnique(sub)):
res.append(sub)
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
Filtered tuples : [(3, 5, 6, 7), (9, 4)]
Method #4:Using filter()+set()+map()
Algorithm:
- Initialize an empty list res to store the filtered tuples.
- Iterate through each tuple sub in test_list.
- Check if the length of the set of elements in sub is equal to the length of sub.
- If the lengths are equal, it means all the elements in sub are unique, so append sub to res.
- Finally, print the res list containing the filtered tuples.
Python3
test_list = [( 3 , 5 , 6 , 7 ), ( 3 , 2 , 4 , 3 ), ( 9 , 4 ), ( 2 , 3 , 2 )]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda tpl: len (tpl) = = len ( set (tpl)), test_list))
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
Filtered tuples : [(3, 5, 6, 7), (9, 4)]
Time complexity: O(n*m), where n is the number of tuples in the list test_list and m is the maximum length of a tuple in test_list. The time complexity of set() is O(n), so the loop over the tuples dominates the overall time complexity.
Auxiliary Space: O(n), where n is the number of tuples in the list test_list. This is because the size of the output list res is proportional to the number of tuples in the input list.
Method#5: Using the Recursive method
Algorithm:
- Define the filter_unique_tuples function that takes in a list test_list as input.
- If the length of test_list is 0, return an empty list.
- Otherwise, extract the first tuple first from test_list and recursively apply filter_unique_tuples to the rest of the list (test_list[1:]), storing the result in rest.
- If the length of the set of values in first is equal to the length of first (meaning all values are unique), append first to rest and return the resulting list ([first] + rest).
- Otherwise, return only the rest.
Python3
def filter_unique_tuples(test_list):
if len (test_list) = = 0 :
return []
else :
first = test_list[ 0 ]
rest = filter_unique_tuples(test_list[ 1 :])
if len ( set (first)) = = len (first):
return [first] + rest
else :
return rest
test_list = [( 3 , 5 , 6 , 7 ), ( 3 , 2 , 4 , 3 ), ( 9 , 4 ), ( 2 , 3 , 2 )]
print ( "The original list is : " + str (test_list))
res = filter_unique_tuples(test_list)
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
Filtered tuples : [(3, 5, 6, 7), (9, 4)]
Time Complexity: O(n^2)
Let n be the length of the input list test_list. In the worst case, all tuples in the list have unique values, so the function needs to examine all n tuples. Each tuple has a length that is at most n, so computing the set of values for each tuple takes time O(n). Therefore, the time complexity of the function is O(n^2).
Auxiliary Space: O(n)
The space complexity of the function is O(n), which is the maximum depth of the recursion stack. This is because each recursive call adds a new frame to the stack, which contains only the variables first and rest.
Method #6: Using dictionary comprehension
we can create a dictionary comprehension where the keys are the tuples themselves and the values are the number of unique elements in the tuple. Then, you can filter the dictionary to only include tuples with the same number of unique elements as the length of the tuple itself.
Python3
test_list = [( 3 , 5 , 6 , 7 ), ( 3 , 2 , 4 , 3 ), ( 9 , 4 ), ( 2 , 3 , 2 )]
their number of unique elements
unique_counts = {t: len ( set (t)) for t in test_list}
res = [t for t, count in unique_counts.items() if count = = len (t)]
print ( "Filtered tuples : " + str (res))
|
Output
Filtered tuples : [(3, 5, 6, 7), (9, 4)]
Time complexity: O(n), where n is the number of tuples in the input list.
Auxiliary space: O(n), since we are creating a dictionary with n key-value pairs.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Apr, 2023
Like Article
Save Article