Wednesday, November 27, 2024
Google search engine
HomeLanguagesPython – Filter unique valued tuples

Python – Filter unique valued tuples

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




# Python3 code to demonstrate working of
# Filter unique valued tuples
# Using loop + set()
 
# initializing list
test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
 
for sub in test_list:
     
    # checking lengths to be equal
    if len(set(sub)) == len(sub):
        res.append(sub)
         
# printing results
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




# Python3 code to demonstrate working of
# Filter unique valued tuples
# Using list comprehension
 
# initializing list
test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# list comprehension used to filter
res = [sub for sub in test_list if len(set(sub)) == len(sub)]
         
# printing results
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




# Python3 code to demonstrate working of
# Filter unique valued tuples
 
# initializing list
test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
 
# printing original list
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)
 
# printing results
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:

  1. Initialize an empty list res to store the filtered tuples.
  2. Iterate through each tuple sub in test_list.
  3. Check if the length of the set of elements in sub is equal to the length of sub.
  4. If the lengths are equal, it means all the elements in sub are unique, so append sub to res.
  5. Finally, print the res list containing the filtered tuples.

Python3




# Python3 code to demonstrate working of
# Filter unique valued tuples
# Using filter() + lambda + set()
# initializing list
 
test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using filter() and lambda to filter tuples with unique values
res = list(filter(lambda tpl: len(tpl) == len(set(tpl)), test_list))
 
# printing results
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:

  1. Define the filter_unique_tuples function that takes in a list test_list as input.
  2. If the length of test_list is 0, return an empty list.
  3. 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.
  4. 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).
  5. Otherwise, return only the rest.

Python3




# Python3 code to demonstrate working of
# Filter unique valued tuples
 
 
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
 
 
# initializing list
test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = filter_unique_tuples(test_list)
# printing results
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)]
 
# create a dictionary of tuples with
their number of unique elements
unique_counts = {t: len(set(t)) for t in test_list}
 
# filter the dictionary to only include tuples
# with the same number of unique elements
# as the length of the tuple itself
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments