Python - Remove Equilength and Equisum Tuple Duplicates
Last Updated : 02 May, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python | Remove duplicates based on Kth element tuple list
Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop Th
8 min read
Python - Remove Kth Index Duplicates in Tuple
Sometimes, while working with Python records, we can have a problem in which we need to remove all the tuples, which have similar Kth index elements in list of records. This kind of problem is common in day-day and web development domain. Let's discuss certain ways in which this task can be performe
7 min read
Python | Sort given list by frequency and remove duplicates
Problems associated with sorting and removal of duplicates is quite common in development domain and general coding as well. The sorting by frequency has been discussed, but sometimes, we even wish to remove the duplicates without using more LOC's and in a shorter way. Let's discuss certain ways in
5 min read
Python | Removing duplicates from tuple
Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 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 - Remove all duplicate occurring tuple records
Sometimes, while working with records, we can have a problem of removing 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() Init
6 min read
Python - Remove Duplicates from a list And Keep The Order
While lists provide a convenient way to manage collections of data, duplicates within a list can sometimes pose challenges. In this article, we will explore different methods to remove duplicates from a Python list while preserving the original order. Using dict.fromkeys()dict.fromkeys() method crea
2 min read
Remove Unordered Duplicate Elements from a List - Python
Given a list of elements, the task is to remove all duplicate elements from the list while maintaining the original order of the elements.For example, if the input is [1, 2, 2, 3, 1] the expected output is [1, 2, 3]. Let's explore various methods to achieve this in Python. Using setWe can initialize
3 min read
Python - Remove Duplicates from a List
Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets
2 min read
Remove Duplicate Strings from a List in Python
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values. [GFGTABS] Python a = ["Learn
3 min read