1

I have:

a=['a','v','c']  
b = ['d','a','c', 'e', 'b']

What I wanted to do was

for x in b:
    if x in a:
        b.remove(x)

But when I run this I get

b=['a','c','b']

I saw elsewhere a few ways you could modify it but none of them seem to be working. For some reason when I add a print statement right below the "for" line. It only prints out 'd', 'c', 'e'. Why are the other two elements not being reached?

I was able to solve this by doing

b = [x for x in b if x in a]

but why wasn't it working in the first place?

John Smith
  • 291
  • 2
  • 12

0 Answers0