0
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?

6
  • 1
    Why does it have to use a list comp? Commented Jul 28, 2018 at 4:54
  • @PM2Ring homework, presumably Commented Jul 28, 2018 at 5:02
  • 1
    Just add an extra condition - or (x[i] != '10') Commented Jul 28, 2018 at 5:03
  • @ andrew_reece It worked ! Commented Jul 28, 2018 at 5:08
  • @Poka great - just moved my comment into an answer, please mark accepted if your problem is resolved. Commented Jul 28, 2018 at 5:56

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.