简体   繁体   中英

getting IndexError: list assignment index out of range

arr = [1]
arr[0] = arr.pop() 

giving me IndexError: list assignment index out of range i don't understand why?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM