Python – Remove all duplicate occurring tuple records
Last Updated :
05 Apr, 2023
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() Initial approach that can be applied is that we can iterate on each tuple and check it’s count in list using count(), if greater than one, we can remove them, converting then to set.
Step-by-step approach :
- Create a list comprehension that iterates over each element ele in test_list
- Check if the count of ele in test_list is greater than 1 (meaning it is a duplicate)
- If it is not duplicate, add it to a set
- Convert the set back into a list and assign it to res
- Print the resulting list of non-duplicate tuples
Below is the implementation of the above approach:
Python3
test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 4 ), ( 3 , 6 ), ( 4 , 5 ), ( 6 , 7 )]
print ("The original list : " + str (test_list))
res = list ( set ([ele for ele in test_list if not test_list.count(ele) > 1 ]))
print (" All the non Duplicate from list are : " + str (res))
|
Output :
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(6, 7), (3, 6)]
Time complexity: O(n^2), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.
Method #2 : Using Counter() + items() + list comprehension The combination of above functions can also be used to perform this particular task. In this, we just get the count of each occurrence of element using Counter() as dictionary and then extract all those whose value is equal to 1.
Python3
from collections import Counter
test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 6 ), ( 3 , 4 ), ( 4 , 5 ), ( 6 , 7 )]
print ("The original list : " + str (test_list))
res = [ele for ele, count in Counter(test_list).items() if count = = 1 ]
print (" All the non Duplicate from list are : " + str (res))
|
Output :
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(6, 7), (3, 6)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method #3: Using operator.countOf() method
Python3
import operator as op
test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 4 ), ( 3 , 6 ), ( 4 , 5 ), ( 6 , 7 )]
print ( "The original list : " + str (test_list))
res = list (
set ([ele for ele in test_list if not op.countOf(test_list, ele) > 1 ]))
print ( "All the non Duplicate from list are : " + str (res))
|
Output
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(6, 7), (3, 6)]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using a dictionary to track occurrences
This method uses a dictionary to track the occurrences of each element in the list. We iterate through the list and increment the count for each element in the dictionary. Then we use a list comprehension to extract all elements with count 1, which are the non-duplicate elements.
Python3
test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 4 ), ( 3 , 6 ), ( 4 , 5 ), ( 6 , 7 )]
print ( "The original list : " + str (test_list))
dict_count = {}
for ele in test_list:
dict_count[ele] = dict_count.get(ele, 0 ) + 1
res = [ele for ele in test_list if dict_count[ele] = = 1 ]
print ( "All the non Duplicate from list are : " + str (res))
|
Output
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(3, 6), (6, 7)]
Time complexity: O(n) because it iterates through the list once to create the dictionary, and then once more to extract the non-duplicate elements using a list comprehension.
Auxiliary space: O(n) because it uses a dictionary to track the occurrences of each element in the list, which can potentially store all the elements of the list.
Method #5: Using a loop and a set to track occurrences
Iterate through the list and use a dictionary to keep track of the items that have already appeared. If an item has not appeared before, we add it to the output list.
Python3
test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 4 ), ( 3 , 6 ), ( 4 , 5 ), ( 6 , 7 )]
print ( "The original list : " + str (test_list))
occurrences = {}
for ele in test_list:
if ele not in occurrences:
occurrences[ele] = 1
else :
occurrences[ele] + = 1
res = [ele for ele in test_list if occurrences[ele] = = 1 ]
print ( "All the non Duplicate from list are : " + str (res))
|
Output
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(3, 6), (6, 7)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), since we use a dictionary to keep track of occurrences.
Method 6: Using list.count() method and list slicing to find non-duplicate items
- Create an empty result list called res.
- Iterate through each item in the original list test_list.
- If the count of the item in test_list is equal to 1, append the item to the result list res.
- Print the result list res.
Python3
test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 4 ), ( 3 , 6 ), ( 4 , 5 ), ( 6 , 7 )]
print ( "The original list : " + str (test_list))
res = [ele for ele in test_list if test_list.count(ele) = = 1 ]
print ( "All the non Duplicate from list are : " + str (res))
|
Output
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(3, 6), (6, 7)]
Time complexity: O(n^2)
Auxiliary space: O(n)
Similar Reads
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 Equilength and Equisum Tuple Duplicates
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 progr
7 min read
Python - Remove nested records from tuple
Sometimes, while working with records, we can have a problem in which an element of a record is another tuple records and we might have to remove the nested records. This is a problem which does not occur commonly, but having a solution to it is useful. Letâs discuss certain way in which this task c
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 - 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 Program To Recursively Remove All Adjacent Duplicates
Given a string, recursively remove adjacent duplicate characters from the string. The output string should not have any adjacent duplicates. See the following examples. Examples: Input: azxxzy Output: ay First "azxxzy" is reduced to "azzy". The string "azzy" contains duplicates, so it is further red
4 min read
Python | Remove duplicate lists in tuples (Preserving Order)
Sometimes, while working with records, we can have a problem in which we need to remove duplicate records. This kind of problem is common in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() In this method, we test fo
7 min read
Python - Remove Duplicate subset Tuples
Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of tuples, which are already present as subsets in other tuples. This kind of problem can be useful in data preprocessing. Let's discuss certain ways in which this task can be performed. Exampl
6 min read
Python List: Remove Duplicates 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
Python - Remove duplicate values in dictionary
Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the removal of all the duplicate values of dictionary, and we are not concerned if any key get removed in the process. This kind of application can occur in school programming and day-day programming.
8 min read