Remove an even/odd number from an odd/even Python list
<p>I am trying to better understand list comprehension in Python.  I completed an online challenge on codewars with a rather inelegant solution, given below.</p>

<p>The challenge was:</p>

<ol>
<li>Given a list of even numbers and one odd, return the odd</li>
<li>Given a list of odd numbers and one even, return the even</li>
</ol>

<p>My (inelegant) solution to this was:</p>

<pre><code>def find_outlier(integers):
    o = []
    e = []
    for i in integers:
        if i % 2 == 0:
            e.append(i)
        else:
            o.append(i)
    # use sums to return int type
    if len(o) == 1:
        return sum(o)
    else:
        return sum(e)
</code></pre>

<p>Which works fine, but seems to be pretty brute force.  Am I wrong in thinking that starting (most) functions with placeholder lists like <code>o</code> and <code>e</code> is pretty "noob-like"?</p>

<p>I would love to better understand why this solution works for the odd list, but fails on the even list, in an effort to better understand list comprehension:</p>

<pre><code>def find_outlier(integers):
    if [x for x in integers if x % 2 == 0]:
       return [x for x in integers if x % 2 == 0]
    elif [x for x in integers if x % 2 != 0]:
       return [x for x in integers if x % 2 != 0]
    else:
        print "wtf!"

o = [1,3,4,5]
e = [2,4,6,7]

In[1]: find_outlier(o)
Out[1]: [4]

In[2]: find_outlier(e)
Out[2]: [2, 4, 6]
</code></pre>

<p>Where <code>Out[2]</code> should be returning <code>7</code>.</p>

<p>Thanks in advance for any insights.</p>

<p>The most efficient answer is going to get a little ugly.</p>

<pre><code>def f(in_list):
    g = (i for i in in_list)
    first = next(g)
    second = next(g) #The problem as described doesn't make sense for fewer than 3 elements.  Let them handle the exceptions.
    if first%2 == second%2:
        a = first%2
        for el in g:
            if el%2 != a:
                return el
    else:
        third = next(g)
        if third%2 == first%2:
            return second
        else:
            return first
    except ValueError('Got a bad list, all evens or all odds')
</code></pre>

python list list-comprehension