0

I have used the below code to remove 20 from list1

list1 = [5, 20, 15, 20, 25, 50, 20]
if 20 in list1:
    list1.remove(20)

It doesn't remove instead if i do it this way first:

list1.remove(20)

but the above line only removes the first occurrence of it but when i run the if loop again, it removes the last occurrence too.

Why if loop doesn't work without that?

list1 = [5, 20, 15, 20, 25, 50, 20]
a = 20
if a in list1:
    list1.remove(a)
>>> print(list1)
[5, 15, 20, 25, 50, 20]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Natan
  • 71
  • 5

0 Answers0