Q:

Why does my code raise an error?

I am doing a coding exercise: Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
So I wrote this code:
def almostIncreasingSequence(sequence):
    first_list, second_list = sequence, sequence
    for i in range(len(sequence)-1):
        if sequence[i] >= sequence[i+1]:
            first_list.remove(sequence[i])
            second_list.remove(sequence[i+1])
            break

    if first_list == sorted(set(first_list)) or second_list == sorted(set(second_list)):
        return True
    else:
        return False

Now this code seems to work on most sequences but this one in particular raises an error:
print almostIncreasingSequence([1,3,2])

The error is as following:
Traceback (most recent call last):
  file.py3 on line ?, in getUserOutputs
    userOutput = _runsppge(testInputs[i])
  file.py3 on line ?, in _runsppge
    return almostIncreasingSequence(*_fArgs_lujxeukjlbwc)
  file.py3 on line 7, in almostIncreasingSequence
    second_list.remove(sequence[i+1])
IndexError: list index out of range

I just dont understand how the list index could possibly be out of range.. Anyone got a clue?

A:

Since you are removing items from the same list which you are iterating on
for i in range(len(sequence)-1):
    if sequence[i] >= sequence[i+1]:
        first_list.remove(sequence[i])     #  <-- removing item
        second_list.remove(sequence[i+1])  #  <-- removing item
        break

And, as mentioned in the comments, first_list, second_list = sequence, sequence doesn't create any new lists. first_list, second_list, and sequence all refer to the exact same list object after that line.
EDITED
The list after the first removal (first_list.remove(sequence[i])), has one less element, hence if the i+1 in on the edge at that moment, it would get out of bound.
Think of a list like [1, 2, 3, 5, 4]. The if statement is triggered when i is at the element 5 (i is 3), then the first removal (first_list.remove(sequence[i])) takes place, after that statement, list has 4 elements [1, 2, 3, 4], and the next line tries to access the element (element 4) at i+1 (second_list.remove(sequence[i+1])), hence it gets (i is 4) out of bound.

