Python

IndexError: List Assignment Index Out of Range Solved

While working on Python, we may encounter an enormous number of mistakes in the console. One of these errors is “IndexError: list assignment index out of range.” This error occurs when a user tries to insert a value into a list in an index or access a value from an index outside of the list’s specified size range. Therefore, we will provide some examples to see how this error arises in Python and how to resolve it. We have been using the Spyder3 Python tool in our Windows 10 environment. So, let’s work on it further with the implementation of the following illustrations:

Example 01

We have created our first example by initializing two lists, i.e., L1 and L2. The list L1 contains five string-type elements in it, while the list L2 is initially empty. Then, we have initialized a “for” loop. Thus, taking the range of a list L1 starting from 0 to the length of the list L1. The “if” statement has been used further to check a string in a list L1. We have been using the string “name” to check if it is already in the list L1 or not. If the condition has been satisfied, the value at the specific index where the value matched would be saved into the list L2 at the same index. As we have not initialized the size of list L2, it will throw an exception. After the “for” loop, both the lists have been printed out:

  • L1 = [“Hi”, “My”, “name” , “is”, “Linux”]
  • L2 = []
  • for n in range(0, len(L1)):
  • if “name” in L1[n]:
  • L2[n] = L1[n]
  • print(L1)
  • print(L2)

When we run the code by utilizing the “Run” button from the Spyder3 menu bar, it throws an exception, i.e., IndexError: list assignment index out of range as displayed in the attached snapshot:

This error can be fixed by using the append() function in the program. The only change is using the append method within the “if” statement of the “for” loop to append the value at a particular index of list L2 by the list L2’s index:

  • L1 = [“Hi”, “My”, “name” , “is”, “Linux”]
  • L2 = []
  • for n in range(0, len(L1)):
  • if “name” in L1[n]:
  • L2.append(L1[n])
  • print(L1)
  • print(L2)

Let’s run the code once again to see if the code works now. The code becomes successful this time, and we have the list L1 and L2 separately as an output as displayed in the attached snapshot:

Example 02

In this example, we will be using a simple empty list to add some value to it. In contrast, the logic would not be defined correctly. So, we have initialized an empty list “L”. The “for” loop has been used with the iterator “I” in the list “L” from its range of 1 to 10. The iterator “I” would be saved into the index “I” of list L. This means the iterator value would be only saved to the index of list L, which would be the same as the iterator “I” value. In the last, the print statement has been utilized to print the updated list L.

  • L = [ ]
  • For i in range (1 , 10) :
  • L [I] = I
  • Print(L)

When we run the above code, it throws an exception that the list assignment index is out of range, as displayed in the attached snapshot:

An append() function can also be used to address the following issues. So, we have updated the code once again. The same empty list “L” has been initialized here. We have defined a different range for the “for” loop iterations, i.e., 2 to 15. Then, the append() method has been used here to append the iterator “I” value to the List. The print statement has been used to print out the list elements:

  • L = [ ]
  • For i in range (2 , 15) :
  • L.append(i)
  • Print(L)

After running the updated code, we have acquired the updated list elements as displayed in the attached snapshot:

Conclusion:

This article contains the implementation of IndexError: list assignment index out of range along with its solutions. We have used the append() method to resolve it, but you can also use the insert() method. We hope this article has provided helpful tips. If you want more tips, please check LinuxHint.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content