How do I remove consecutive duplicates from a list?
<p>How do I remove consecutive duplicates from a list like this in python?</p>

<pre><code>lst = [1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]
</code></pre>

<p>Having a unique list or set wouldn't solve the problem as there are some repeated values like 1,...,1 in the previous list.</p>

<p>I want the result to be like this:</p>

<pre><code>newlst = [1,2,4,1,3,5]
</code></pre>

<p>Would you also please consider the case when I have a list like this 
 <code>[4, 4, 4, 4, 2, 2, 3, 3, 3, 3, 3, 3]</code> 
and I want the result to be <code>[4,2,3,3]</code> 
rather than <code>[4,2,3]</code> .</p>

<p>If you want to use the <code>itertools</code> method @MaxU suggested, a possible code implementation is:</p>

<pre><code>import itertools as it

lst=[1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]

unique_lst = [i[0] for i in it.groupby(lst)]

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

python