Python | Remove Consecutive tuple according to key
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 next(), 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.
Python3
# Python3 code to demonstrate working of # Remove Consecutive tuple according to key # using itemgetter() + next() + groupby() from operator import itemgetter from itertools import groupby # initialize list test_list = [( 4 , 5 ), ( 4 , 6 ), ( 7 , 8 ), ( 7 , 1 ), ( 7 , 0 ), ( 8 , 1 )] # printing original list print ("The original list is : " + str (test_list)) # Remove Consecutive tuple according to key # using itemgetter() + next() + groupby() res = [ next (group) for key, group in groupby(test_list, key = itemgetter( 0 ))] # printing result print (" List after Consecutive tuple removal : " + str (res)) |
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)]
Method#2 : Using loop()
In this code, we loop over each tuple in the original list and check if its first element is different than the previous tuple’s first element (or if it’s the first tuple). If it is, we add it to the filtered list. If it’s not, we skip it. This way, we only keep the first tuple with each unique first element in the original list. Finally, we print the filtered list.
Python3
# Define the original list original_list = [( 4 , 5 ), ( 4 , 6 ), ( 7 , 8 ), ( 7 , 1 ), ( 7 , 0 ), ( 8 , 1 )] # Initialize an empty list to hold the filtered items filtered_list = [] # Loop over each tuple in the original list for i in range ( len (original_list)): # Check if this tuple has a different first element than the previous one (or if it's the first one) if i = = 0 or original_list[i][ 0 ] ! = original_list[i - 1 ][ 0 ]: filtered_list.append(original_list[i]) # Print the filtered list print (filtered_list) |
[(4, 5), (7, 8), (8, 1)]
Time complexity: O(n)
Auxiliary Space: O(k)
Please Login to comment...