I have a list which has these elements(list consists str(elements)):
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
and I want to process it to have an output like this
['-0', '1#', '15#']
If the element is -x i want to leave it there so I take the last 2 elements and if the gap is 1 then remove the element before the last element. Here is the code:
for k in range(len(l1)):
if "-" in (l1[-k] or l1[-k-1]):
print("debuggggg")
pass
elif(int(l1[-k]) - int(l1[-k-1])== 1 and int(l1[-k]) - int(l1[-k-2])== 2) :
a= l1[-2]
print(a)
l1.remove(a)
#print("debug 2")
elif(int(l1[-k]) - int(l1[-k-1])== 1):
a= l1[-2]
l1.remove(a)
l1[-2] = l1[-2] +"#"
l1[-1] = l1[-1] +"#"
print("3")
#elif(type(l1[-2]) is str):
#pass
Problem is here :
debuggggg
14
13
12
11
10
9
8
The last 2 elements do not include char "-" but it seems that they do. Furthermore after the 8 loop script crashes:
elif(int(l1[-k]) - int(l1[-k-1])== 1 and int(l1[-k]) - int(l1[-k-2])== 2) :
IndexError: list index out of range
but it is not out the range of the list. What is the problem?