-1

list=[5, 14, 98, 55] i=0 for x in list: if x%5 != 0: list.remove(x) print(list) Only 14 gets deleted, while 98 stays in the list... By now I've tried anything really...

lin000
  • 11
  • 1
    Does this answer your question? [Strange result when removing item from a list while iterating over it](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list-while-iterating-over-it) – Brian61354270 Apr 03 '21 at 21:15

1 Answers1

1

Give this a try. It creates a new list which only contains numbers divisible by 5.

listx=[5, 14, 98, 55]
newlistX = [x for x in listx if x % 5 == 0 ]
print(newlistX)

Buddy Bob
  • 5,829
  • 1
  • 13
  • 44