1

I am a python programmer and I was using the remove function from a list. The problem is that it shows me the item that I am trying to remove. Here is the code that I wrote.

set1 = [10, 10, 10, 2, 4, 0]

for i in set1:
    if i < 9:
        set1.remove(i)
print(set1)

And this is the output

[10, 10, 10, 4]

Why does it show a 4? What can I do?

  • Try `set1 = set(i for i in set1 if i >=10)` – wakey Nov 18 '20 at 13:47
  • 2
    You should not modify a list as you are iterating through it. An alternative in this case would be using a list comprehension `set1 = [i for i in set1 if i >= 9]` – Cory Kramer Nov 18 '20 at 13:47

0 Answers0