Python | Remove duplicates based on Kth element tuple list
Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of tuple in list. This problem has application in domains which uses records as input. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop
This is a brute force method to perform this particular task. In this, we check for the Kth index of tuple and add to a set to keep record. If that value is already in the memory set, we discard it from result as it’s duplicate.
# Python3 code to demonstrate working of # Remove duplicates based on Kth element tuple list # Using loop # initialize list test_list = [(3, 1, 5), (1, 3, 6), (2, 1, 7), (5, 2, 8), (6, 3, 0)] # printing original list print("The original list is : " + str(test_list)) # initialize K K = 1 # Remove duplicates based on Kth element tuple list # Using loop temp = set() res = [] for ele in test_list: if ele[K] not in temp: res.append(ele) temp.add(ele[K]) # printing result print("The list after removal of K based duplicates : " + str(res)) |
The original list is : [(3, 1, 5), (1, 3, 6), (2, 1, 7), (5, 2, 8), (6, 3, 0)]
The list after removal of K based duplicates : [(3, 1, 5), (1, 3, 6), (5, 2, 8)]
Method #2 : Using reduce() + lambda + keys()
In this method, we perform the task of filtering using reduce() + lambda, and decide to append or not using the extracted keys using keys(). If key has already occurred, its discarded otherwise added.
# Python3 code to demonstrate working of # Remove duplicates based on Kth element tuple list # Using reduce() + lambda + keys() from functools import reduce # initialize list test_list = [(3, 1), (1, 3), (3, 2), (5, 2), (5, 3)] # printing original list print("The original list is : " + str(test_list)) # initialize K K = 0 # Remove duplicates based on Kth element tuple list # Using reduce() + lambda + keys() res = reduce(lambda sub, ele : ele[K] in dict(sub).keys() and sub or sub + [ele], test_list, []) # printing result print("The list after removal of K based duplicates : " + str(res)) |
The original list is : [(3, 1), (1, 3), (3, 2), (5, 2), (5, 3)]
The list after removal of K based duplicates : [(3, 1), (1, 3), (5, 2)]
Recommended Posts:
- Python | Remove particular element from tuple list
- Python | Sort tuple based on occurrence of first element
- Python | Group tuple into list based on value
- Python | Sort tuple list by Nth element of tuple
- Python | Remove Duplicates from a List
- Python | Remove similar element rows in tuple Matrix
- Python | Ways to remove duplicates from list
- Python | Remove consecutive duplicates from list
- Python | Remove duplicates from nested list
- Python | Remove tuple from list of tuples if not containing any character
- Python | Sort given list by frequency and remove duplicates
- Python | Remove all duplicates and permutations in nested list
- Python | Minimum element in tuple list
- Python | Update each element in tuple list
- Python | Maximum element in tuple list
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.


