How can I find the position in a list where an item differs from the previous item?
<pre><code>l1 = [1,2,2,2]
</code></pre>

<p>In the list above, I know that the element differs in l1[0]. Is there any way to find the position in python?</p>

<p>Using a modification of <code>itertools</code> recipe <code>unique_everseen</code> for a solution that keeps track of new entries in a list.</p>

<pre><code>from itertools import ifilterfalse
def unique_everseen(iterable, key=None, per_index=False):
    "List unique elements, preserving order. Remember all elements ever seen. 
    Use per_index=True to return indices instead of elements."
    # unique_everseen('AAAABBBCCDAABBB') --&gt; A B C D
    # unique_everseen('ABBCcAD', str.lower) --&gt; A B C D
    # unique_everseen('ABBCcAD', str.lower, True) --&gt; 0 1 3 6
    seen = set()
    seen_add = seen.add
    if key is None:
        for index, element in enumerate(ifilterfalse(seen.__contains__, iterable)):
            seen_add(element)
            yield element if not per_index else index
    else:
        for index, element in enumerate(iterable):
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element if not per_index else index
</code></pre>

<p>To try it out on your example:</p>

<pre><code>list(unique_everseen(l1, per_index=True)) # returns [0, 1]
</code></pre>

python