Python | Extract unique tuples from list, Order Irrespective
Last Updated :
22 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to check for similar records and eradicate them. When elements are ordered, this case has been discussed before. But sometimes, we might have to ignore the order and has to be removed in case similar elements occur. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + set()
The combination of above functionalities can be clubbed to perform this particular task. In this, we check for each pair and add to a set for reference to check if it has existed before and add if it’s a new one.
Python3
test_list = [( 1 , 3 ), ( 4 , 5 ), ( 3 , 1 ), ( 1 , 10 ), ( 5 , 4 )]
print ( "The original list : " + str (test_list))
res = set ()
temp = [res.add((a, b)) for (a, b) in test_list
if (a, b) and (b, a) not in res]
print ( "The list after duplicated removal : " + str ( list (res)))
|
Output :
The original list : [(1, 3), (4, 5), (3, 1), (1, 10), (5, 4)]
The list after duplicated removal : [(4, 5), (1, 3), (1, 10)]
Method #2: Using frozenset()
Another method to perform this particular task is to use frozenset(). This function eliminates internally the similar elements irrespective of order.
Python3
test_list = [( 1 , 3 ), ( 4 , 5 ), ( 3 , 1 ), ( 1 , 10 ), ( 5 , 4 )]
print ( "The original list : " + str (test_list))
res = set ( tuple ( frozenset (sub)) for sub in set (test_list))
print ( "The list after duplicated removal : " + str ( list (res)))
|
Output :
The original list : [(1, 3), (4, 5), (3, 1), (1, 10), (5, 4)]
The list after duplicated removal : [(4, 5), (1, 3), (1, 10)]
Method #3: Use a dictionary to track unique tuples:
Approach:
- Create an empty dictionary unique_dict to store unique tuples.
- Iterate over each tuple in the input list input_list.
- Convert each tuple into a sorted tuple using tuple(sorted(tup)) and use it as a key in the dictionary unique_dict. The value of the key is the original tuple tup.
- Return the list of values of the unique_dict.
Python3
def extract_unique_tuples_dict(input_list):
unique_dict = {}
for tup in input_list:
unique_dict[ tuple ( sorted (tup))] = tup
return list (unique_dict.values())
input_list = [( 1 , 3 ), ( 4 , 5 ), ( 3 , 1 ), ( 1 , 10 ), ( 5 , 4 )]
unique_tuples = extract_unique_tuples_dict(input_list)
print (f "The original list : {input_list}" )
print (f "The list after duplicated removal : {unique_tuples}" )
|
Output
The original list : [(1, 3), (4, 5), (3, 1), (1, 10), (5, 4)]
The list after duplicated removal : [(3, 1), (5, 4), (1, 10)]
Time complexity: O(n log n) for sorting the tuples and O(n) for iterating over the input list and accessing the dictionary. The overall time complexity is O(n log n).
Auxiliary Space: O(n) for storing the unique tuples in the dictionary.
Method #4: Using numpy.unique() function.
- Create a list comprehension to sort each tuple in the input list.
- Pass the sorted tuples to the `np.unique()` function with the axis parameter set to 0 to get the unique sorted tuples.
- Create a list comprehension to convert each unique sorted tuple to a tuple of integers and append it to a result list.
- Return the result list of unique tuples.
Python3
import numpy as np
def extract_unique_tuples_dict(input_list):
return [ tuple (x) for x in np.unique([ sorted (tup) for tup in input_list], axis = 0 )]
input_list = [( 1 , 3 ), ( 4 , 5 ), ( 3 , 1 ), ( 1 , 10 ), ( 5 , 4 )]
unique_tuples = extract_unique_tuples_dict(input_list)
print (f "The original list : {input_list}" )
print (f "The list after duplicated removal : {unique_tuples}" )
|
Output
The original list : [(1, 3), (4, 5), (3, 1), (1, 10), (5, 4)]
The list after duplicated removal : [(3, 1), (5, 4), (1, 10)]
Time Complexity:
The time complexity of this algorithm is O(n log n), where n is the length of the input list. This is because the algorithm sorts each tuple in the input list using the built-in `sorted()` function, which has a time complexity of O(k log k), where k is the length of the tuple. Then, the `np.unique()` function has a time complexity of O(n log n) as it sorts the array of sorted tuples before identifying the unique values. Finally, the conversion of each unique sorted tuple to a tuple of integers has a time complexity of O(k), which is constant for each tuple.
Space Complexity:
The space complexity of this algorithm is O(n), where n is the length of the input list. This is because the algorithm creates an array of sorted tuples with length n, and a result list of unique tuples with length less than or equal to n. The space complexity of the `np.unique()` function depends on the number of unique sorted tuples in the array, but it should not exceed the space complexity of the result list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...