list index out of range, whenever i change the list size

  • Last Update :
  • Techknowledgy :

For example, to create a list of names you would do the following:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

len() will return an integer, which will be the number of items stored in the list.

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

#create a variable called name_length to store the length of the list
name_length = len(names)

#print value of variable to the console
print(name_length)

#output
#4

Taking the example from earlier, this is how you would access each item inside the list using its index number:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

Let's go back to the list we've used so far.

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

Say I want to access the last item, "Lenny", and try to do so by using the following code:

print(names[4])

#output

#Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_articles/demo.py", line 3, in <module>
   # print(names[4])
   #IndexError: list index out of range

Suggestion : 2

String IndexError: List Index Out of Range,Tuple IndexError: List Index Out of Range,One frequent bug in Python is the IndexError: list index out of range. So, what does this error message mean?,To solve the “IndexError: list index out of range”, avoid do not access a non-existing list index. For example, my_list[5] causes an error for a list with three elements.

Let’s have a look at an example where this error arises:

lst = ['Alice', 'Bob', 'Carl']
print(lst[3])

Here’s an example of wrong code that will cause the error to appear:

# WRONG CODE
lst = ['Alice', 'Bob', 'Ann', 'Carl']

for i in range(len(lst)+1):
lst[i]


# Traceback (most recent call last):
# File "C:\Users\xcent\Desktop\code.py", line 5, in <module>
   # lst[i]
   # IndexError: list index out of range

So, let’s insert a print statement before that line:

lst = ['Alice', 'Bob', 'Ann', 'Carl']

for i in range(len(lst) + 1):
   print(i)
lst[i]

The correct code is, therefore:

# CORRECT CODE
lst = ['Alice', 'Bob', 'Ann', 'Carl']

for i in range(len(lst)):
   lst[i]

The IndexError also frequently occurs if you iterate over a list but you remove elements as you iterate over the list:

l = [1, 2, 3, 0, 0, 1]
for i in range(0, len(l)):
   if l[i] == 0:
   l.pop(i)

Suggestion : 3

“List index out of range” error occurs in Python when we try to access an undefined element from the list.,We have seen the different situations where the list index out of range error might occur. You can check the length of the list using before performing operations or using the indexes.,If you are working with lists in Python, you have to know the index of the list elements. This will help you access them and perform operations on them such as printing them or looping through the elements. But in case you mention an index in your code that is outside the range of the list, you will encounter an IndexError.,indentationerror: unindent does not match any outer indentation level in Python

Example: 

# Declaring list
list_fruits = ['apple', 'banana', 'orange']
# Print value of list at index 3
print(list_fruits[3]);

Output:

Traceback (most recent call last):
  File "list-index.py", line 2, in <module>
       print(list_fruits[3]);
   IndexError: list index out of range

Correct Example: 

# Declaring list
list_fruits = ['Apple', 'Banana', 'Orange']
# Print list element at index 2
print(list_fruits[2]);

1. Example with "while" Loop

# Declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i = 0
#
while loop less then and equal to list "list_fruits"
length.
while i <= len(list_fruits):
   print(list_fruits[i])
i += 1

Example

# declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i = 0
#
while loop less then and equal to list "list_fruits"
length.
while i <= len(list_fruits):
   # print the value of i
print(i)
# print value of list
print(list_fruits[i])
i += 1

Suggestion : 4

This problem frequently occurs when trying to index the end of a list.,Recall that indexing in Python starts at zero. We're getting this error because we've gone outside the range of the list. Even though the list has a length of ten, its indexes only range from 0-9, making the tenth index out of range.,We can fix the error by altering the index used to return the final list value, shown in the following solution:,We see this error when indexing a list while using a value outside the range of indexes for the list. Today we'll take a look at some of the most common causes of this error, along with how to solve it using some practical examples.

IndexError: list index out of range
example_list = [4, 7, 1, 0, 8, 1, 4, 9, 7, 3]

last_index = len(example_list) # 10

final_value = example_list[last_index]

print(final_value)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-6-68e9a1660bcf> in <module>
      3 last_index = len(example_list) # 10
      4
      ----> 5 final_value = example_list[last_index]
      6
      7 print(final_value)
      IndexError: list index out of range
example_list = [4, 7, 1, 0, 8, 1, 4, 9, 7, 3]

last_index = len(example_list) - 1 # 9

final_value = example_list[last_index]

print(final_value)
3
example_list = [4, 7, 1, 0, 8, 1, 4, 9, 7, 3]

final_value = example_list[-1]

print(final_value)