Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove sequential repeats from python list

I have a list of tuples. First element is index. I need to delete tuples which has subsequent indexes only leaving first tuple from each sequence:

In:
[
  [0, 100],
  [1, 200],
  [5, 600],
  [6, 300],
  [7, 800],
  [9, 300],
  [11, 100],
  [14, 300],
]

Out:
[
  [0, 100],
  [5, 600],
  [9, 300],
  [11, 100],
  [14, 300],
]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Using a simple loop (and assuming l the input list):

prev = -float('inf')
out = []
for x in l:
    if x[0]>prev+1:
        out.append(x)
    prev=x[0]

output: [[0, 100], [5, 600], [9, 300], [11, 100], [14, 300]]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading