Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] [Resolve] IndexError: List Assignment Index Out of Range

#1
[Resolve] IndexError: List Assignment Index Out of Range



When does the IndexError: list assignment index out of range appear?

Python throws an IndexError if you try to assign a value to a list index that doesn’t exist, yet. For example, if you execute the expression list[1] = 10 on an empty list, Python throws the IndexError. Simply resolve it by adding elements to your list until the index actually exists.


Here’s the minimal example that throws the IndexError:

lst = []
lst[1] = 10

If you run this code, you’ll see that Python throws an IndexError:

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 range

You can resolve it by adding two “dummy” elements to the list so that the index 1 actually exists in the list:

lst = [None, None]
lst[1] = 10
print(lst)

Now, Python will print the expected output:

[None, 10]

Try to fix the IndexError in the following interactive code shell:

Exercise: Can you fix this code?

So what are some other occurrences of the IndexError?

IndexError in For Loop


Frequently, the IndexError happens if you use a for loop to modify some list elements like here:

# WRONG CODE:
lst = []
for i in range(10): lst[i] = i
print(lst)

Again, the result is an IndexError:

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 range

You modify a list element at index i that doesn’t exist in the list. Instead, create the list using the list(range(10)) list constructor.

# CORRECT CODE:
lst = list(range(10))
print(lst)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

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!



https://www.sickgaming.net/blog/2020/04/...-of-range/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python IndexError: Tuple Index Out of Range [Easy Fix] xSicKxBot 0 1,944 08-22-2023, 09:07 AM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,455 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] Python Find Shortest List in List xSicKxBot 0 1,368 09-25-2022, 03:42 AM
Last Post: xSicKxBot
  [Tut] Python Find Longest List in List xSicKxBot 0 1,298 09-23-2022, 02:19 PM
Last Post: xSicKxBot
  [Tut] How to Convert an Integer List to a Float List in Python xSicKxBot 0 1,321 12-15-2020, 01:03 AM
Last Post: xSicKxBot
  [Tut] Python range() Function — A Helpful Illustrated Guide xSicKxBot 0 1,383 12-12-2020, 10:22 PM
Last Post: xSicKxBot
  [Tut] list.clear() vs New List — Why Clearing a List Rather Than Creating a New One? xSicKxBot 0 1,338 11-30-2020, 06:53 PM
Last Post: xSicKxBot
  [Tut] How to Convert a Float List to an Integer List in Python xSicKxBot 0 1,253 11-11-2020, 06:35 PM
Last Post: xSicKxBot
  [Tut] List Changes After Assignment — How to Clone or Copy It? xSicKxBot 0 1,342 09-26-2020, 09:14 PM
Last Post: xSicKxBot
  [Tut] Dict to List — How to Convert a Dictionary to a List in Python xSicKxBot 0 1,436 06-30-2020, 01:53 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016