How to make exceptions during the iteration of a for loop in python
<p>Sorry in advance for the certainly simple answer to this but I can't seem to figure out how to nest an <code>if ______ in ____:</code> block into an existing <code>for</code> block.</p>

<p>For example, how would I change this block to iterate through each instance of <code>i</code>, omitting odd numbers. </p>

<pre><code>odds = '1 3 5 7 9'.split()
for i in range(x):
   if i in odds: 
      continue
   print(i)
</code></pre>

<p>this code works for <code>if i == y</code> but I cannot get it to work with a specific set of "y"s</p>

<p>If you are looking to iterate over a range of even numbers, then something like this should work. With X being an integer. The <code>2</code> is the step so this will omit odd numbers.</p>

<pre><code>for i in range(0,x,2):
    print(i)
</code></pre>

<p>For more info check out the docs here:</p>

<p><a href="https://docs.python.org/2/library/functions.html#range" rel="nofollow">https://docs.python.org/2/library/functions.html#range</a></p>

<p>I ran in to a couple of problems with the code you provided, continue would just fall through to the print statement and the values in <code>odds</code> where chars not ints which meant the comparison failed.</p>

<p>Creating a list of integers and using <code>not in</code> instead of <code>in</code> will get around this.</p>

<pre><code>x = 10
odds = [1, 3, 5, 9]
for i in range(x):
    if i not in odds:
        print(i)
</code></pre>

python python-3.x if-statement for-loop nested