0
primeList=[5, 7, 35, 67, 335, 469]
for i in primeList:
        for x in range(2,i):
            if (i%x)==0:
                print(primeList)
                primeList.remove(i)
print(primeList)

I want to remove all of the elements in the list that are not prime but for some reason this only seems to remove 35 when 335 and 469 should be removed.

Derek
  • 1
  • The dup link will tell you why you can't modify a list that you are iterating, but the right way to do this is to build a NEW list that only includes the members you want to keep. – Tim Roberts Apr 28 '22 at 16:47

0 Answers0