0

I had a problem with removing items from a list with python. So I played a little bit and noticed that '.remove()' doesn't work as I would expect.

I created a list with strings and numbers:

list = [1, 2, 3, 4, "a", "b", "c", "d"]

Now I i look at every item and if it is a string I would like to remove it.

print(list)

for i in list:
    print(str(i) + str(type(i)))
    if type(i) == str:
        list.remove(i)

print(list)

And here is the output:

[1, 2, 3, 4, 'a', 'b', 'c', 'd'] 
1<class 'int'> 
2<class 'int'> 
3<class 'int'> 
4<class 'int'> 
a<class 'str'> 
c<class 'str'> 
[1, 2, 3, 4, 'b', 'd'] 

Why is 'b' and 'd' still in the list?

Peon
  • 1
  • 2

0 Answers0