0
K = int(input())
m = int(input())
a = []
c = []
for i in range (1, K+1):
    a.append(i)
for i in range(m):
    b = int(input())
    c.append(b)

for j in a:
    for i in c:
        if a[j] % c[i] == 0:
             a.remove(a[j])
print(a)

Currently giving me a index out of range error, but I don't see whats wrong with it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Anonmly
  • 161
  • 1
  • 5
  • 12

1 Answers1

2

You're modifying a list while iterating over it. Generally speaking, that's not a good idea.

The root problem is the same as in this question (and many others that you could find by searching Stack Overflow).


Additionally, you're using the items in the list as the indices instead of the items themselves. You probably meant:

for j in a:
    for i in c:
        if j % i == 0:
            ...
Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696