I have a list which contains numbers in ascending order like
[0, 1, 3, 4, 6]
or
[2, 3, 4, 5]
I need to get a list of lists which contains the elements as a range items like
[[start, end], [start, end]]
from the input list.
[[0, 1], [3, 4], [6, 6]]
or
[[2, 5]]
where the elements that are not present in the input list should.
Here is the code I tried. But not sure how to get it.
zers = [0, 1, 3, 4, 6]
ls = []
for i, j in enumerate(zers):
if i!=len(zers)-1 and zers[i+1]==zers[i]+1:last=i;continue
else:
if i==0:ls.append([i,i])
else:ls.append([last, i])
print(ls)
It is supposed to give [[0, 1], [3, 4], [6, 6]]
But giving [[0, 1], [2, 3], [2, 4]]
Please feel free to make any modifications to my code or provide completely different solution.
I'm thinking that there should be a function from an existing library but just not sure. Please let me know if you comes across such.