if elif else blocks evaluate for every case
<p>I am trying to append an iterator to a list but my code below evaluates for every case.</p>

<pre><code>Less7=Head7=Over7=[]
i=0

for i in range(0,10):
    if i&lt;7:
        Less7.append(i)

    elif i==7:
        Head7=i

    else:
        Over7.append(i)
</code></pre>

<p>The result I am getting are:
Head7 is an int value of 7
Less7 and Over7 are lists - [0,1,2,3,4,5,6,7,8,9]</p>

<p>My desired results are:</p>

<pre><code>Less7=[0,1,2,3,4,5,6]

Head7=[7]

Over7=[8,9]
</code></pre>

<p>I'm sure it's basic, could you point me in the right direction?
My thought is that it has to do with the datatype.
When I step through the code, even <code>Head7</code> evaluates <code>[0,1,2,3,4,5,6]</code> but when <code>i=7</code> then it correctly assigns the value, but I want it in a list.</p>

<p>You need to create <em>three lists</em>, one for each possible outcome:</p>

<pre><code>less_than_7, is_7, greater_than_7 = [], [], []

for i in range(0, 10):
    if i &lt; 7:
        less_than_7.append(i)
    elif i &gt; 7:
        greater_than_7.append(i)
    else:
        is_7.append(i)
</code></pre>

<p><em><code>Less7=Head7=Over7=[]</code></em> creates one list that is referenced by the three names so your output would be identical as you are appending to the same list, well you are in the first and last case, <em><code>Head7=i</code></em> sets <em><code>Head7</code></em> equal to <em><code>i/7</code></em>.</p>

python python-3.5.2