I have tried to answer this question changing as little of your code as possible.
x=[1,2,3,4,5,8,11,12,13,18]
temp=[]
firstnumber=0
first_index = 0
for i in range(1, len(x)):
current=x[i]
previous=x[i-1]
if ((current-previous)!=1):
mm = x[first_index:i]
temp.append(mm)
firstnumber = x[i]
first_index = i
temp.append(x[first_index:])
print(temp) # [[1, 2, 3, 4, 5], [8], [11, 12, 13], [18]]
What I changed:
firstnumber
is being used as an index, but in reality is an element of the list, so we need to use first_index = i
, the current index on that iteration.
The loop did not cover all the elements of the list, we need to go all the way to the end of the list so we iterate over range(1, len(x)
Finally even if the loop completes it will be missing the last sequence unless we add it after the loop, hence the addition of temp.append(x[first_index:])
NOTE: This method will work with the input you have but it not robust for all cases, nor is it the most efficient way to do this, however, your question was why it did not work as is so hopefully this answers that.