how do i solve "list index out of range" error in python? (IndexError)
Question:
I tried to create a sorting algorithm but it didn’t work.
Here’s my code:
from random import *
sort = [9, 7, 4, 5, 8, 3, 2, 1, 6, 10]
def sort_array(array):
for i in range(len(array)):
if array[i + 1] < array[i + 2]:
array[i + 1], array[i + 2] = array[i + 2], array[i + 1]
else:
pass
print(array)
sort_array(sort)
I get this error when I’m trying to run it:
if array[i + 1] < array[i + 2]: IndexError: list index out of range
I was expecting it to just sort the array.
Answers:
You should try an iteration until the array length – 2 since i+2 will go out of bound throwing an error try with for i in range(len(array)-2)
.
Anyway i don’t think your code will work.
Read about Sorting algorithms or if you’re too lazy there’s a spoiler:
sort = [9, 7, 4, 5, 8, 3, 2, 1, 6, 10]
def sort_array(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
print(arr)
sort_array(sort)
Well, if you print(i)
inside of your loop (it is better to use log), you will see that it reaches 8 and then breaks. You have a list of length 10, thus its maximum index value can be 9, while you are calling array[8 + 2] which doesn’t exist and it is out of your list’s bounds.
Moreover, your current sorting algorithm doesn’t move the zero element (in your case "9"). FYI, I don’t think that your array will be sorted in one loop, so you may want to modify your algorithm.
P.S.
- Don’t import this way
from random import *
. It is a bad practice.
- Check
enumerate(array)
instead of range(len(array))
. It is a better practice
I tried to create a sorting algorithm but it didn’t work.
Here’s my code:
from random import *
sort = [9, 7, 4, 5, 8, 3, 2, 1, 6, 10]
def sort_array(array):
for i in range(len(array)):
if array[i + 1] < array[i + 2]:
array[i + 1], array[i + 2] = array[i + 2], array[i + 1]
else:
pass
print(array)
sort_array(sort)
I get this error when I’m trying to run it:
if array[i + 1] < array[i + 2]: IndexError: list index out of range
I was expecting it to just sort the array.
You should try an iteration until the array length – 2 since i+2 will go out of bound throwing an error try with for i in range(len(array)-2)
.
Anyway i don’t think your code will work.
Read about Sorting algorithms or if you’re too lazy there’s a spoiler:
sort = [9, 7, 4, 5, 8, 3, 2, 1, 6, 10]
def sort_array(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
print(arr)
sort_array(sort)
Well, if you print(i)
inside of your loop (it is better to use log), you will see that it reaches 8 and then breaks. You have a list of length 10, thus its maximum index value can be 9, while you are calling array[8 + 2] which doesn’t exist and it is out of your list’s bounds.
Moreover, your current sorting algorithm doesn’t move the zero element (in your case "9"). FYI, I don’t think that your array will be sorted in one loop, so you may want to modify your algorithm.
P.S.
- Don’t import this way
from random import *
. It is a bad practice. - Check
enumerate(array)
instead ofrange(len(array))
. It is a better practice