![]() |
VOOZH | about |
Sometimes, while working with Python records, we can have a problem in which we need to extract only the unique tuples, based on some particular index of tuples. This kind of problem can have applications in domains such as web development. Let's discuss certain ways in which this task can be performed.
Input : test_list = [(5, 6, 5), (4, 2, 7), (1, 2, 3), (9, 6, 5)] K = 3
Output : [(1, 2, 3), (5, 6, 5), (4, 2, 7)]Input : test_list = [(5, ), (1, ), (1, ), (9, )] K = 1
Output : [(1, ), (5, ), (9, )]
Method #1 : Using map() + next() + lambda The combination of above functions can be used to solve this problem. In this, we extend the logic of getting unique elements extracted using lambda function and next(), using map().
The original list is : [(5, 6, 8), (4, 2, 7), (1, 2, 3), (9, 6, 5)] The extracted elements : [(4, 2, 7), (5, 6, 8)]
Time Complexity: O(n*n) where n is the number of elements in the in the list “test_list”. The map() + next() + lambda is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n), the algorithm uses an additional list to store the result, thus consuming linear space which is O(n).
Method #2 : Using next() + groupby() + lambda The combination of above functions can also be used to solve this problem. In this, we perform task of map() in above using groupby(), in a more compact way.
The original list is : [(5, 6, 8), (4, 2, 7), (1, 2, 3), (9, 6, 5)] The extracted elements : [(4, 2, 7), (5, 6, 8)]
Time Complexity: O(n*logn), where n is the length of the input list test_list.
Auxiliary Space: O(n)
Method #3 : Using loops ,in and not in operators
The original list is : [(5, 6, 8), (4, 2, 7), (1, 2, 3), (9, 6, 5)] The extracted elements : [(5, 6, 8), (4, 2, 7)]
Time complexity: O(n^2) in the worst case, where n is the length of the input list
Auxiliary space: O(n) for the list a and the result list res, where n is the length of the input list.
Method 4: Using a set and a list comprehension
The original list is : [(5, 6, 8), (4, 2, 7), (1, 2, 3), (9, 6, 5)] The extracted elements : [(5, 6, 8), (4, 2, 7)]
Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(n), where n is the length of test_list.
Method #5: Using a dictionary and a loop
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [(5, 6, 8), (4, 2, 7), (1, 2, 3), (9, 6, 5)] The extracted elements : [(5, 6, 8), (4, 2, 7)]
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(n), to store the dictionary unique_dict.
Method #6: Using reduce():
Algorithm:
The original list is : [(5, 6, 8), (4, 2, 7), (1, 2, 3), (9, 6, 5)] The extracted elements : [(5, 6, 8), (4, 2, 7)]
Time Complexity: O(n), where n is the length of the input list.
The time complexity of the for loop is O(n) and the time complexity of the dictionary lookups and list append operations inside the loop is O(1).
Space Complexity: O(n), where n is the length of the input list.
The space complexity is dominated by the dictionary unique_dict and the list res, which can each potentially store up to n elements.
Method #7 : Using filter() and lambda function
The original list is : [(5, 6, 8), (4, 2, 7), (1, 2, 3), (9, 6, 5)] The extracted elements : [(5, 6, 8), (4, 2, 7)]
Time complexity: O(n)
Auxiliary space: O(n) for the set to keep track of unique Kth index values.