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>Check if the next element always is not equal to item. If so append.</p>

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

new_item = lst[0]
new_list = [lst[0]]
for l in lst:
   if new_item != l:
     new_list.append(l)
     new_item = l

print new_list
print lst
</code></pre>

python