what is most pythonic way to find a element in a list that is different with other elements?
<p>Suppose we have a list with unknown size and there is an element in the list that is different with other elements but we don't know the index of the element. the list only contains numerics and is fetched from a remote server and the length of the list and the index of the different element is changed every time. what is the most pythonic way to find that different element?
I tried this but I'm not sure if it's the best solution.</p>

<pre><code>a = 1
different_element = None
my_list = fetch_list()

b = my_list[0] - a

for elem in my_list[1::]:
    if elem - a != b:
        different_element = elem

print(different_element)
</code></pre>

<p>This is a great use for <a href="http://www.numpy.org" rel="nofollow">numpy</a></p>

<p>Given some random uniform list with a single uniquely different number in it:</p>

<pre><code>&gt;&gt;&gt; li=[1]*100+[200]+[1]*250
</code></pre>

<p>If the uniform value is known (in this case 1 and the unknown value is 200) you can use <code>np.where</code> on an array to get that value:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; a=np.array(li)
&gt;&gt;&gt; a[a!=1]
array([200])
</code></pre>

<p>If the uniform values are not known, you can use <code>np.uniques</code> to get the counts of uniques:</p>

<pre><code>&gt;&gt;&gt; np.unique(a, return_counts=True)
(array([  1, 200]), array([350,   1]))
</code></pre>

<p>For a pure Python solution, use a generator with <code>next</code> to get the first value that is different than all the others:</p>

<pre><code>&gt;&gt;&gt; next(e for i, e in enumerate(li) if li[i]!=1)
200
</code></pre>

<p>Or, you can use <a href="https://docs.python.org/2/library/itertools.html#itertools.dropwhile" rel="nofollow">dropwhile</a> from itertools:</p>

<pre><code>&gt;&gt;&gt; from itertools import dropwhile
&gt;&gt;&gt; next(dropwhile(lambda e: e==1, li))
200
</code></pre>

<p>If you do not know what the uniform value is, use a Counter on a slice big enough to get it:</p>

<pre><code>&gt;&gt;&gt; uniform=Counter(li[0:3]).most_common()[0][0]
&gt;&gt;&gt; uniform
1
&gt;&gt;&gt; next(e for i, e in enumerate(li) if li[i]!=uniform)
200
</code></pre>

<p>In these cases, <code>next</code> will short-circuit at the first value that satisfies the condition. </p>

python