Reputation: 387
I have a list
x=['b1','00','00','10','10','F5','D1','01','01'...] # sample data
I am trying to remove duplicates of '10' only when they are adjacent.
Till now I have tried
my_list= [x[i] for i in range(len(x)) if (i==0) or ( x[i] !=x[i-1])] # current implementation
This removes all adjacent duplicates but I want to keep '00','00' and '01' and just remove duplicates of '10'
How do I achieve this using a list comprehension?
Upvotes: 0
Views: 77
Reputation: 21264
Just add an extra condition to your list comprehension: or (x[i] != '10')
.
[x[i] for i in range(len(x)) if (i==0) or (x[i] !=x[i-1]) or (x[i] != '10')]
# ['b1', '00', '00', '10', 'F5', 'D1', '01', '01']
Upvotes: 1