title: Remove consecutive duplicates from nested list in Python?
body: <p>I have a nested list that has the following structure:</p>
  
  <pre><code>mylist = [['A', 'Car', '15'], ['A', 'Car', '15'], ['A', 'Plane', '16'], ['A', 'Bike', '20'], ['A', 'Car', '16'], ['A', 'Boat', '16']]
  </code></pre>
  
  <p>It's super long, with around 10 million elements. And I have many of these lists. What I want to do is:</p>
  
  <p><em>If the third items (the string numbers) of each consecutive element from <code>mylist</code> are  duplicates, remove the elements that contain this duplicate.</em></p>
  
  <p>For example:</p>
  
  <p><code>['A', 'Car', '15']</code> and <code>['A', 'Car', '15']</code> are consecutive elements from <code>mylist</code>, and they both contain <code>'15'</code>, so they are consecutive duplicates, and one should be removed. </p>
  
  <p>Similarly, <code>['A', 'Car', '16']</code> and <code>['A', 'Boat', '16']</code> are consecutive and both contain <code>'16'</code>, so one should be removed.</p>
  
  <p>So, what I would end up with is:</p>
  
  <pre><code>newlist = [['A', 'Car', '15'], ['A', 'Plane', '16'], ['A', 'Bike', '20'], ['A', 'Car', '16']]
  </code></pre>
  
  <p>I initially wrote this:</p>
  
  <pre><code>for ele in mylist:
      eleindex = mylist.index(ele)
      previousele = mylist[eleindex-1]
      if float(ele[2]) != float(previousele[2]):
          newlist.append(ele)
  </code></pre>
  
  <p>Unfortunately, the code I wrote took way to long for such long lists. So, I began looking online and learned that the <code>itertools</code> library (using <code>groupby</code>) is useful and <strong>very fast</strong> at doing these kinds of things. I then found some examples that I tried emulating, however, they were mainly for simple lists - not something a little more complicated like my situation. After tinkering around, I wasn't able to figure out how to use it for my nested lists.</p>
  
  <p>So, does anyone know how to do this very quickly? Also, if you have a solution that will be faster than <code>itertools</code>, that's even better!</p>