title: Only store item in list if different from the first or last item
body: <p>I've got a multidimensional list like this:</p>
  
  <p>[[1525815456, 2],[1525815487, 3],[1525815517, 2],[1525815547, 2],[1525815577, 2],[1525815608, 4],[1525815638, 4]]</p>
  
  <p>How can I loop through it and delete any lists in the list where the second value is the same as either the last or next second value but not both? So that the list above would change to this:</p>
  
  <p>[[1525815456, 2],[1525815487, 3],[1525815517, 2],[1525815577, 2],[1525815608, 4],[1525815638, 4]]</p>
  
  <p>I have already tried something like this:</p>
  
  <pre><code>for idx in range(len(data)-1):
      x = data[idx]
      if data[idx][1] != data[idx-1][1] and data[idx][1] == data[idx+1][1]:
          newdata.append(x)
      if data[idx][1] != data[idx+1][1] and data[idx][1] == data[idx-1][1]:
          newdata.append(x)
      if data[idx][1] != data[idx+1][1] and data[idx][1] != data[idx-1][1]:
          newdata.append(x)
  </code></pre>
  
  <p>But I keep getting errors and cannot get it to work.</p>
  
  <p>Edit:</p>
  
  <p>Thanks everyone, here's the code:</p>
  
  <pre><code>data = [[1, 2],[2, 3],[3, 2],[4, 2],[5, 2],[6, 4],[7, 4]]
  newdata = []
  for idx in range(len(data)-1):
      x = data[idx]
      if idx != 0 and idx != len(data)-1:
          if data[idx][1] != data[idx-1][1] or data[idx][1] != data[idx+1][1]:
              newdata.append(x)
      else:
          newdata.append(x)
  newdata.append(data[-1])
  print newdata
  </code></pre>
  
  <p>will return:</p>
  
  <pre><code>[[1, 2], [2, 3], [3, 2], [5, 2], [6, 4], [7, 4]]
  </code></pre>