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 particular element from tuple list
- Python | Sort tuple based on occurrence of first element
- Python | Group tuple into list based on value
- Python | sort list of tuple based on sum
- Python | Sort tuple list by Nth element of tuple
- Python Remove Duplicates from a List
- Python | Remove consecutive duplicates from list
- Python | Ways to remove duplicates from list
- Python | Remove duplicates from nested list
- Python | Remove similar element rows in tuple Matrix
- Python | Remove all duplicates and permutations in nested list
- Python | Sort given list by frequency and remove duplicates
- Python | Remove tuple from list of tuples if not containing any character
- Python | Maximum element in tuple list
- Python - Concatenate Kth 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 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.