Open In App

Python Indexerror: list assignment index out of range Solution

Last Updated : 30 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In python, lists are mutable as the elements of a list can be modified. But if you try to modify a value whose index is greater than or equal to the length of the list then you will encounter an Indexerror: list assignment index out of range.  

Python Indexerror: list assignment index out of range Example

If ‘fruits’ is a list, fruits=[‘Apple’,’ Banana’,’ Guava’]and you try to modify fruits[5] then you will get an index error since the length of fruits list=3 which is less than index asked to modify for which is 5.

Python3




# list of fruits
fruits = ['Apple', 'Banana', 'Guava'
 
# to check type of fruits
print("Type is", type(fruits)) 
fruits[5] = 'Mango'


Output:

Traceback (most recent call last):
File "/example.py", line 3, in <module>
fruits[5]='Mango'
IndexError: list assignment index out of range

So, as you can see in the above example, we get an error when we try to modify an index that is not present in the list of fruits.

Python Indexerror: list assignment index out of range Solution

Method 1: Using insert() function

The insert(index, element) function takes two arguments, index and element, and adds a new element at the specified index.

Let’s see how you can add Mango to the list of fruits on index 1.

Python3




fruits = ['Apple', 'Banana', 'Guava']
print("Original list:", fruits)
 
fruits.insert(1, "Mango")
print("Modified list:", fruits)


Output:

Original list: ['Apple', 'Banana', 'Guava']
Modified list: ['Apple', 'Mango', 'Banana', 'Guava']

It is necessary to specify the index in the insert(index, element) function, otherwise, you will an error that the insert(index, element) function needed two arguments.

Method 2: Using append()

The append(element) function takes one argument element and adds a new element at the end of the list.

Let’s see how you can add Mango to the end of the list using the append(element) function.

Python3




fruits = ['Apple', 'Banana', 'Guava']
print("Original list:", fruits)
 
fruits.append("Mango")
print("Modified list:", fruits)


Output:

Original list: ['Apple', 'Banana', 'Guava']
Modified list: ['Apple', 'Banana', 'Guava', 'Mango']

Python IndexError FAQ

Q: What is an IndexError in Python?

A: An IndexError is a common error that occurs when you try to access an element in a list, tuple, or other sequence using an index that is out of range. It means that the index you provided is either negative or greater than or equal to the length of the sequence.

Q: How can I fix an IndexError in Python?

A: To fix an IndexError, you can take the following steps:

  1. Check the index value: Make sure the index you’re using is within the valid range for the sequence. Remember that indexing starts from 0, so the first element is at index 0, the second at index 1, and so on.
  2. Verify the sequence length: Ensure that the sequence you’re working with has enough elements. If the sequence is empty, trying to access any index will result in an IndexError.
  3. Review loop conditions: If the IndexError occurs within a loop, check the loop conditions to ensure they are correctly set. Make sure the loop is not running more times than expected or trying to access an element beyond the sequence’s length.
  4. Use try-except: Wrap the code block that might raise an IndexError within a try-except block. This allows you to catch the exception and handle it gracefully, preventing your program from crashing.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads