Sometimes, while working with Python list, we can have a problem in which we can have a list of tuples and we wish to remove them basis of first element of tuple to avoid it’s consecutive duplication. Let’s discuss certain way in which this problem can be solved.
Method : Using
groupby() + itemgetter() + next()
This task can be performed using combination of these functions. In this, we convert the list to iterator for faster access using
itemgetter() is used to get the index of tuple on which we need to perform the deletion(in this case first) and
groupby() performs the final grouping of elements.
The original list is : [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)] List after Consecutive tuple removal : [(4, 5), (7, 8), (8, 1)]
- Python | Replace tuple according to Nth tuple element
- Python | Sort tuple list by Nth element of tuple
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Tuple key detection from value list
- Python | Tuple key dictionary conversion
- Python | Check if tuple exists as dictionary key
- Python | Test if key exists in tuple keys dictionary
- Python - Custom Tuple Key Summation in Dictionary
- Python - Convert Nested Tuple to Custom Key Dictionary
- Python - Sort dictionary by Tuple Key Product
- Python - Concatenate Tuple to Dictionary Key
- Python - Extract target key from other key values
- Python - Filter key's value from other key
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python - Concatenate consecutive elements in Tuple
- Python - Consecutive Tuple difference
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.