04-19-2020, 07:15 PM
[Resolve] IndexError: List Assignment Index Out of Range
When does the appear?
Python throws an if you try to assign a value to a list index that doesn’t exist, yet. For example, if you execute the expression on an empty , Python throws the . Simply resolve it by adding elements to your list until the index actually exists.
![[Image: image-2.png]](https://blog.finxter.com/wp-content/uploads/2020/04/image-2.png)
Here’s the minimal example that throws the IndexError:
If you run this code, you’ll see that Python throws an:
You can resolve it by adding two “dummy” elements to the list so that the index 1 actually exists in the list:
Now, Python will print the expected output:
Try to fix the in the following interactive code shell:
https://repl.it/@finxter/RemarkablePunge...?lite=true
Exercise: Can you fix this code?
So what are some other occurrences of the IndexError?
IndexError in For Loop
Frequently, the happens if you use a loop to modify some list elements like here:
Again, the result is an:
You modify a list element at index that doesn’t exist in the list. Instead, create the list using the list constructor.
Where to Go From Here?
You’ve learned how to resolve one error. By doing this, your Python skills have improved a little bit. Do this every day and soon, you’ll be a skilled master coder.
Do you want to leverage those skills in the most effective way? In other words: do you want to earn money with Python?
If the answer is yes, let me show you a simple way how you can create your simple, home-based coding business online:
Join Free Webinar: How to Become a Six-Figure Coder as an Average Coder?
Start your new thriving coding business now!
When does the
Code:
IndexError: list assignment index out of rangePython throws an
Code:
IndexErrorCode:
list[1] = 10Code:
listCode:
IndexError![[Image: image-2.png]](https://blog.finxter.com/wp-content/uploads/2020/04/image-2.png)
Here’s the minimal example that throws the IndexError:
Code:
lst = []
lst[1] = 10If you run this code, you’ll see that Python throws an
Code:
IndexErrorCode:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 2, in <module> lst[1] = 10
IndexError: list assignment index out of rangeYou can resolve it by adding two “dummy” elements to the list so that the index 1 actually exists in the list:
Code:
lst = [None, None]
lst[1] = 10
print(lst)Now, Python will print the expected output:
Code:
[None, 10]Try to fix the
Code:
IndexErrorhttps://repl.it/@finxter/RemarkablePunge...?lite=true
Exercise: Can you fix this code?
So what are some other occurrences of the IndexError?
IndexError in For Loop
Frequently, the
Code:
IndexErrorCode:
forCode:
# WRONG CODE:
lst = []
for i in range(10): lst[i] = i
print(lst)Again, the result is an
Code:
IndexErrorCode:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 4, in <module> lst[i] = i
IndexError: list assignment index out of rangeYou modify a list element at index
Code:
iCode:
list(range(10))Code:
# CORRECT CODE:
lst = list(range(10))
print(lst)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]You’ve learned how to resolve one error. By doing this, your Python skills have improved a little bit. Do this every day and soon, you’ll be a skilled master coder.
Do you want to leverage those skills in the most effective way? In other words: do you want to earn money with Python?
If the answer is yes, let me show you a simple way how you can create your simple, home-based coding business online:
Join Free Webinar: How to Become a Six-Figure Coder as an Average Coder?
Start your new thriving coding business now!

