IndexError: list index out of range

0

I'm creating a program to sort a list of numbers.

However, the program returns me:

  

IndexError: list index out of range

This is my code:

number_list=[]

list_lenght=int(input("List lenght: "))

while len(number_list)<list_lenght:
    item=input("Enter new item to the list:")
    number_list.append(item)
    print(number_list)

print("That's your number list: ",number_list)

number_list_final=[]

def order_number_list(number_list):
    i=0
    i2=i+1
    while (i2)<=(len(number_list)):
        while number_list[i]<=number_list[i2]:
            i2=i2+1
        i=i2
        i2=i+1
    final_item=number_list[i]
    number_list_final.append(final_item)
    del number_list[i] 
    order_number_list(number_list)

order_number_list(number_list)
print(number_list_final)

Does anyone know the reason?

    
asked by Gonzalo Molina 18.01.2017 в 15:04
source

3 answers

2

In this code snippet:

while i2 <= len(number_list):
    while number_list[i] <= number_list[i2]:
        i2 = i2+1

In the first while , i2 finally reaches as value an index that does not exist. You have to change the condition to avoid the last value.

On the other hand, the second while is increasing i2 without checking if the end of the list has been reached.

Likewise, the list you pass as an argument is altered in the process by losing elements as you do del number_list[i] . Irremediably there will come a time when the list is empty and that this del produces error.

Assuming you want to make your own sorting algorithm, without using python facilities like sorted , min or max , still let me advise you a couple of things:

  • Never, never, never use external variables as a result of a function. Every function should return its result by return
  • Use for before while to go through a list
  • Modifying a list while it is being processed is often the cause of numerous errors. The list that you pass to order is destroyed inside the function, shortening in length and losing elements.

Maintaining the function as recursive (which would not be necessary), a possible rewrite would be like this:

def order_number_list(number_list):

    if len(number_list) == 0:
        return number_list
    else:
        m = number_list[0]

        for i in range(1, len(number_list)):
            if m > number_list[i]:
                m = number_list[i]

        return [m] + order_number_list(number_list[1:])

number_list_final = order_number_list(number_list)   

Although no recursion is better:

def order_number_list(number_list):

    lst = number_list[:]  # copia de la lista

    for i in range(1, len(lst)):
        for j in range(i, len(lst)): 
            if lst[i] > lst[j]:
                lst[i], lst[j] = lst[j], lst[i]

    return lst
    
answered by 18.01.2017 / 18:55
source
1

in the first while of the order_number_list method, change the condition

answered by 18.01.2017 в 15:39
0

remember that in Python the fixes start from index 0. Then, for example, this arrangement: [1,54,23,6] has 4 elements, but 1 is in index 0, 54 is in 1, 23 is in 2 and 6 is in 3. Then, when you do:

    while (i2)<=(len(number_list)): ...

The last value of i2 will be the number of elements in the list (in our example 4), however we know that index 4 does not exist! Since it only reaches 3 (from 0).

Then the solution is to change the condition either by:

    while (i2) <= (len(number_list) - 1): ...

or by:

    while (i2) < (len(number_list)): ...

I hope I have helped:)

    
answered by 25.01.2017 в 05:33