title: How to get unique values from list using previous/current/next?
body: <p>I have implemented a program which takes a list, and returns unique values based on the previous, current and next elements.</p>
  <p>However, the program does not work when the list has more than two of the same element in the beginning.</p>
  <p>Ex: <code>[0, 0, 0, 1]</code></p>
  <p>Here is the code, followed by a description of the mechanics.</p>
  <pre><code>def test(current):
      
      iterator = iter(current)
      previous_item = next(iterator)
      current_item = next(iterator)
      next_item = True
      
      
      while next_item:
          
          try:
              next_item = next(iterator)       
          except StopIteration:
              next_item = None      
          
          if previous_item == current_item:
              current_item = next_item
  
          else:
              yield previous_item
              previous_item = current_item
              current_item = next_item
  
          
      if previous_item:
          yield previous_item
  
  
          
  if __name__ == &quot;__main__&quot;:
      
      l = [0, 0, 1, 2]
      
      for u in test(l):
          print(u)
  </code></pre>
  <p>The loop terminates if <code>next_item</code> is <code>None</code>. I only yield a <code>previous_item</code> if <code>previous_item</code> is not equal to <code>current_item</code>. I also only move the <code>previous_item</code> &quot;pointer&quot; if <code>previous_item</code> is not equal to <code>current_item</code>. <code>previous_item</code> is always guaranteed to have a value, so I am confused why inputs such as <code>[0, 0, 0, 1]</code> do not yield anything.</p>
  <p>Regards,</p>