I am trying to check if 2 elements in a list are incrementing by 1.
For instance, in a list like this [1,3,4,5,6,7,9,11,12]
, my code should return a new list like this [3,4,5,6,11,12]
This is my code:
same = [1,3,4,5,6,7,9,11,12]
new_datalist = []
index = 0
for k in same:
try:
new_index = index+1
if k+1 == same[new_index]:
new_datalist.append(k)
new_datalist.append(k+1)
index += 1
except:
pass
new_datalist
But it is returning this - [3, 4, 4, 5, 5, 6, 6, 7, 11, 12]
How do I solve this?
Just to explain further:
Let's look at it like this, elements in the list represents the monthly salary of a worker. I want to check if the worker receives half-salary for 2 consecutive months. For example, a list like this [3,4,5,6,7,8,1,44]
would return a list like [3,4,5,6,7,8]
. This means the worker receives half-salary for months 3&4, 5&6 and 7&8