Open In App

Python – Remove Equilength and Equisum Tuple Duplicates

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python tuples, we can have a problem in which we need to remove duplicates on basis of equal length and equal sum. This kind of problem can also be broken to accommodate any one of the required conditions. This kind of problem can occur in data domains and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(1, 2, 0), (3, 0), (2, 1)] 
Output : [(1, 2, 0), (3, 0)]

Input : test_list = [(1, 2, 0), (3, 0, 0), (0, 2, 1)] 
Output : [(1, 2, 0)] 

Method #1: Using nested loops 
This is one of the ways in which this task can be performed. This is the brute force method, in which we loop for each tuple, a matching tuple w.r.t size and sum, and perform the removal.

Python3




# Python3 code to demonstrate working of
# Remove Equilength and Equisum Tuple Duplicates
# Using nested loop
 
# initializing lists
test_list = [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove Equilength and Equisum Tuple Duplicates
# Using nested loop
res = []
for sub in test_list:
    for sub1 in res:
        if len(sub) == len(sub1) and sum(sub) == sum(sub1):
            break
    else:
        res.append(sub)
 
# printing result
print("Tuples after filtration : " + str(res))


Output

The original list is : [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
Tuples after filtration : [(4, 5, 6), (3, 0), (1, 2, 3)]

Time complexity: O(n^2), where n is the length of the input list. This is because the program has a nested loop, where each element of the input list is compared with each element of the result list.
Auxiliary space: O(n), where n is the length of the input list. This is because the program creates a new list to store the filtered tuples, which can be at most the same length as the input list.

Method #2 : Using dict() + values() 
The combination of the above functions offers another way to solve this problem. In this, we just harness the property of the dictionary of having unique keys and create a tuple key with len and sum of tuples. The duplicates are thus, avoided.

Python3




# Python3 code to demonstrate working of
# Remove Equilength and Equisum Tuple Duplicates
# Using dict() + values()
 
# initializing lists
test_list = [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove Equilength and Equisum Tuple Duplicates
# Using dict() + values()
res = list({(len(sub), sum(sub)): sub for sub in test_list}.values())
 
# printing result
print("Tuples after filtration : " + str(res))


Output

The original list is : [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
Tuples after filtration : [(5, 5, 5), (2, 1), (1, 2, 3)]

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(m), where m is the number of unique tuples in the input list.

Method #3: Removing Equilength and Equisum Tuple Duplicates is to use a set to keep track of unique tuples.

One alternative method to solve the problem of removing Equilength and Equisum Tuple Duplicates is to use a set to keep track of unique tuples. For each tuple in the input list, we can create a new tuple that consists of its length and sum, and check if it already exists in the set. If it does not exist, we add the original tuple to the output list and add the new tuple to the set.

Python3




test_list = [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
 
# using a set to keep track of unique tuples
seen = set()
res = []
 
for t in test_list:
    # create a new tuple that consists of length and sum
    key = (len(t), sum(t))
    # check if the new tuple is already in the set
    if key not in seen:
        # add the original tuple to the output list
        res.append(t)
        # add the new tuple to the set
        seen.add(key)
 
print(res)


Output

[(4, 5, 6), (3, 0), (1, 2, 3)]

Time complexity: O(n), where n is the length of the input list,
Auxiliary space: O(n), since we may need to store all n tuples in the output list and n unique (length, sum) tuples in the set.

Method #4: Using list comprehensions and lambda functions

This code snippet creates a lambda function compute_key to compute the length and sum of a tuple. It then uses a list comprehension to generate a new list res with unique tuples. The if condition in the list comprehension checks whether the computed key of the current tuple is already present in the keys of the computed keys of the previous tuples. If it’s not present, the current tuple is added to the output list.

Python3




test_list = [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
 
# create a lambda function to compute the length and sum of a tuple
compute_key = lambda x: (len(x), sum(x))
 
# use list comprehension to generate a new list with unique tuples
res = [x for i, x in enumerate(test_list) if compute_key(x) not in map(compute_key, test_list[:i])]
 
print(res)


Output

[(4, 5, 6), (3, 0), (1, 2, 3)]

Time complexity: O(n^2) because it uses a list comprehension with an enumerate function to iterate over each tuple in the input list and a map function to compute the keys of the previous tuples.
Auxiliary space: O(n) because it uses a new list res to store the unique tuples. 

Method #5: Using a dictionary and tuple manipulation

This method involves using a dictionary to keep track of unique tuples based on their length and sum. The tuples are first converted to a string and then used as keys in the dictionary. If a tuple with the same length and sum is encountered, it is not added to the final list. Otherwise, the tuple is added to the dictionary and the final list.

Python3




# Python3 code to demonstrate working of
# Remove Equilength and Equisum Tuple Duplicates
# Using dictionary and tuple manipulation
 
# initializing lists
test_list = [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove Equilength and Equisum Tuple Duplicates
# Using dictionary and tuple manipulation
unique_tuples = {}
res = []
for tup in test_list:
    tup_str = str(sorted(tup))
    tup_len = len(tup)
    tup_sum = sum(tup)
    if tup_str not in unique_tuples:
        unique_tuples[tup_str] = (tup_len, tup_sum)
        res.append(tup)
 
# printing result
print("Tuples after filtration : " + str(res))


Output

The original list is : [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
Tuples after filtration : [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]

Time complexity: O(nlogn), where n is the number of tuples in the input list. 
Auxiliary space: O(n), where n is the number of tuples in the input list.

 Method #6 using itertools.groupby()

Python3




import itertools
 
test_list = [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
 
# sort the list by length and then by sum
test_list.sort(key=lambda x: (len(x), sum(x)))
 
# use itertools.groupby() to group tuples with the same length and sum
res = [next(group) for _, group in itertools.groupby(test_list, key=lambda x: (len(x), sum(x)))]
 
print(res)


Output

[(3, 0), (1, 2, 3), (4, 5, 6)]

Time complexity: O(n*logn) because of the sorting operation. 
Auxiliary space: O(n) space to store the output list. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads