because you pop'd from the list, now the list has zero elements, then you try to assign to arr[0], but since there is not arr[0], you get the index error.
– juanpa.arrivillaga
2 Answers
7
votes
By calling arr.pop() it makes arr change to [], so after that if you try retrieving / chaging item at index 0 it will be out of range since its size is 0
0
votes
The right-hand side of an assignment statement is evaluated before the left-hand side. So the list is empty when you try to assign at index 0.
You could rewrite your code roughly as:
arr = [1]
popped = arr.pop()
arr[0] = popped
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
pop
'd from the list, now the list has zero elements, then you try to assign toarr[0]
, but since there is not arr[0], you get the index error. – juanpa.arrivillaga