Open In App

Python – Remove Kth Index Duplicates in Tuple

Improve
Improve
Like Article
Like
Save
Share
Report

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 performed.

Input : test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)], K = 1 
Output : [(4, 5, 6), (2, 3, 4), (7, 6, 4), (1, 2, 6)] 
Input : test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)], K = 0 
Output : [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4)]

Method #1: Using loop

This is one of the ways in which this task can be performed. In this, we perform the task of memoizing the already occurred Kth index element in set, and check each time with a new value.

Python3




# Python3 code to demonstrate working of
# Remove Kth Index Duplicates in Tuple
# Using loop
 
# initializing lists
test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# Remove Kth Index Duplicates in Tuple
# Using loop
keep = set()
res = []
for sub in test_list:
    if sub[K] not in keep:
        res.append(sub)
        keep.add(sub[K])
 
# printing result
print("Filtered Tuples : " + str(res))


Output : 

The original list is : [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
Filtered Tuples : [(4, 5, 6), (2, 3, 4)]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the number of distinct values in the Kth index of the input tuples. 

Method #2: Using groupby() + itemgetter() + list comprehension

The combination of the above functions can be used to solve this problem. In this, we perform the task of checking for Kth element using itemgetter() and groupby() is used to form groups, out of which we extract 1st element by accessing 0th index. 

Python3




# Python3 code to demonstrate working of
# Remove Kth Index Duplicates in Tuple
# Using groupby() + itemgetter() + list comprehension
from itertools import groupby
from operator import itemgetter
 
# initializing lists
test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# Remove Kth Index Duplicates in Tuple
# Using groupby() + itemgetter() + list comprehension
res = res = [list(val)[0] for key, val in groupby(sorted(test_list,
                                                         key=itemgetter(K)), key=itemgetter(K))]
 
# printing result
print("Filtered Tuples : " + str(res))


Output : 

The original list is : [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
Filtered Tuples : [(4, 5, 6), (2, 3, 4)]

Time complexity: O(n log n)
where n is the length of the input list test_list. This is because the program sorts the list using the sorted() function, which has a worst-case time complexity of O(n log n), and then applies groupby() function to group the tuples based on the Kth index. The groupby() function has a worst-case time complexity of O(n), where n is the length of the input iterable.

Auxiliary space: O(n)
Where n is the length of the input list test_list. This is because the program creates a new list res that stores the filtered tuples, which can have a maximum length of n.

Method #3: Using filter()

Python3




# Python3 code to demonstrate working of
# Remove Kth Index Duplicates in Tuple
# Using filter
 
# initializing lists
test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# Remove Kth Index Duplicates in Tuple
# Using filter
keep = set()
res = list(filter(lambda sub: sub[K] not in keep and (keep.add(sub[K]) or True), test_list))
 
# printing result
print("Filtered Tuples : " + str(res))
#This code is contributed by vinay Pinjala.


Output

The original list is : [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
Filtered Tuples : [(4, 5, 6), (2, 3, 4)]

Time complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using a dictionary

Create a dictionary where the keys are the elements of the Kth index of each tuple and the values are the tuples themselves. Then, iterate over the dictionary and select only one tuple per key to create the final list.

Python3




# initializing lists
test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
 
# initializing K
K = 2
 
# Remove Kth Index Duplicates in Tuple
# Using dictionary
d = {}
for tpl in test_list:
    if tpl[K] not in d:
        d[tpl[K]] = tpl
res = list(d.values())
 
# printing result
print("Filtered Tuples : " + str(res))


Output

Filtered Tuples : [(4, 5, 6), (2, 3, 4)]

Time Complexity: The loop that iterates over the tuples in the test_list has a time complexity of O(n).
Auxiliary Space: O(m) because we are creating a dictionary that has at most m key-value pairs, where m is the number of unique elements in the Kth index of the tuples.

Method#5: Using Recursive method.

The algorithm for the recursive method to remove Kth index duplicates in a list of tuples is as follows:

  1. Define a function remove_kth_duplicates that takes in three arguments: test_list, K, and keep.
  2. Check if test_list is empty.
  3. If test_list is empty, return an empty list.
  4. Otherwise, check if the Kth element of the first tuple in test_list is not in the set keep.
  5. If the Kth element of the first tuple in test_list is not in the set keep, add it to the set keep and return a new list consisting of the first tuple in test_list followed by the result of calling
  6. remove_kth_duplicates recursively with the remaining tuples in test_list.
  7. Otherwise, call remove_kth_duplicates recursively with the remaining tuples in test_list.

Python3




def remove_kth_duplicates(test_list, K, keep=set()):
    if not test_list:
        return []
    elif test_list[0][K] not in keep:
        keep.add(test_list[0][K])
        return [test_list[0]] + remove_kth_duplicates(test_list[1:], K, keep)
    else:
        return remove_kth_duplicates(test_list[1:], K, keep)
 
test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
K = 2
 
print("The original list is : " + str(test_list))
res = remove_kth_duplicates(test_list, K)
print("Filtered Tuples : " + str(res))


Output

The original list is : [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
Filtered Tuples : [(4, 5, 6), (2, 3, 4)]

The time complexity of this algorithm is O(n), where n is the number of tuples in test_list. This is because we need to iterate over all tuples in test_list to remove duplicates.

The auxiliary space of this algorithm is O(n), where n is the number of tuples in test_list. This is because we need to store n recursive calls on the call stack and create a new list to store the result.

Method 6:  Using Set

Approach:

  1. Initialize an empty set to keep track of the Kth elements that have been seen before.
  2. Initialize an empty list to store the filtered tuples.
  3. Loop through each tuple in the test_list.
  4. Check if the Kth element of the current tuple is not in the set. If so, add it to the set and append the current tuple to the filtered list.
  5. Return the filtered list.

Python3




def remove_kth_duplicates(test_list, K):
    if not test_list:
        return []
    seen_kth = set()
    filtered_list = []
    for tup in test_list:
        if tup[K] not in seen_kth:
            seen_kth.add(tup[K])
            filtered_list.append(tup)
    return filtered_list
 
test_list = [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
K = 2
 
print("The original list is : " + str(test_list))
res = remove_kth_duplicates(test_list, K)
print("Filtered Tuples : " + str(res))


Output

The original list is : [(4, 5, 6), (2, 3, 4), (1, 3, 4), (7, 6, 4), (1, 2, 6)]
Filtered Tuples : [(4, 5, 6), (2, 3, 4)]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the number of unique Kth elements in the input list.



Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads