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 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 already occurred Kth index element in set, and check each time with new value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


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)]

 

Method #2 : Using groupby() + itemgetter() + list comprehension
The combination of 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


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)]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.