title: How can I avoid the list error function in my code as I alter a list I am iterating?
body: <p>I am looking to use pop and remove functions to remove numbers from the list 2 to 100 in order to get a list of prime numbers. The main problem is that k always ends up causing an error. Also, when a put a print function after <code>k</code>, it shows up only even numbers, not sure why that is happening.   </p>
  
  <pre><code>x=[] 
  for i in range(2,100): 
      x.append(i)
  
  primes=[]
  
  count=0
  
  while count &lt; 99:
      k = x[count]
      print(k)
      primes.append(k)
      """for j in range(2,100):
          if k % j ==0:
              x.remove(j)"""
      x.pop(count)
      count = count + 1
  
  print(x)
  </code></pre>