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.
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.
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)]
- Python - Remove Kth Index Duplicates in Tuple
- Python - Remove Equilength and Equisum Tuple Duplicates
- Python | Sort tuple list by Nth element of tuple
- Python - Concatenate Kth element in Tuple List
- Python - Extract Kth element of every Nth tuple in List
- Python | Replace tuple according to Nth tuple element
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Remove particular element from tuple list
- Python | Adding N to Kth tuple element
- Python - Closest Pair to Kth index element in Tuple
- Python | Sort tuple based on occurrence of first element
- Python | Summation of Kth Column of Tuple List
- Python - Kth Column Product in Tuple List
- Python - Kth Index Tuple List Mean
- Python - Consecutive Kth column Difference in Tuple List
- Python | Group tuple into list based on value
- Python | sort list of tuple based on sum
- Python | Replace duplicates in tuple
- Python | Removing duplicates from tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.