0

I'm new to python and I wanted to know why my program is displaying "IndexError: list index out of range" for line 4. Can anyone please help.

# A is the array and N is the size of the array.
A =[1,78,46,4,34,10,50,2]
N = len(A)
def Algorithm(A,N):
    #B <- Array[N]
    B = A[N]
    B=[0]*N
    for i in range(1,N):
        B[A[i]]+=1
        i=1
    #for i <-- 1 to N
    for j in range(1,N):
    #for k <-- to B[j]
        for k in range(0,B[j]):
            A[i]=j
            i+=1
    return

Algorithm(A,N)
print(A)

Error:

  2 N = len(A)
  3 def Algorithm(A,N):
  4     B = A[N]
  5     B=[0]*N
  6     for i in range(1,N):

IndexError: list index out of range

2
  • 2
    list indices go from 0 up to and including array length minus 1. Commented Apr 2, 2020 at 14:49
  • 1
    A has eight elements. A[8] does not exist, because Python lists are zero-indexed. Valid indexes for A are 0 thru 7. Commented Apr 2, 2020 at 14:51

3 Answers 3

1

So the List index out of range comes from B = A[N] because N represents the total length of A. However, elements of a list are indexed from 0 up to N-1, which gives you the length (N-1) - 0 + 1 => N. If you want to assign B to the last element of A, you can do that by B = A[N-1], or B = A[-1], since negative indices point to elements of the list from the end. However, given you redeclare B = [0] * N, you could do away with the first assignment

Sign up to request clarification or add additional context in comments.

Comments

0

In Python, the first element of a list is addressed as zero rather than one. For example, to access the first element in a list called "numbers" you should write numbers[0] rather than numbers[1].

In B = A[N], you're trying to address the last element of list A by accessing A[N] (N corresponds to the length of list A). However, as previously explained, you have to minus one from this because the list starts at zero and not one. Therefore, the correct code would be B = A[N - 1].

5 Comments

@Student It would be helpful if you could explain what the intended purpose of the algorithm is because although my answer fixes the error you have mentioned in your question, there are additional errors after that which I cannot help you fix without further information. For example, on one line you create B and assign it the value 2 (the last element in list A) but then in the next line you replace B so there seems to be little point in the first line.
I'm trying to convert a pseudocode algorithm to python. Here's the pseudocode: ` Algo(A,N) B<- Array[N] B[1..N] <-0 for i <- 1 to N B[A[i]]++ i <- i for j <-1 to N for k <- 0 to B[j] A[i] <- j i++ return`
@Student Thanks I'll take a look at it.
@Student Have you considered the possibility that A isn't a list of numbers but instead a list of lists? I can't see otherwise how that algorithm makes sense because the line B<-Array[N] sets B to an integer (if we presume A is a list of numbers) and then later on the in the algorithm it tries to treat B as an array, e.g. B[1..N]<-0 and B[A[i]]++.
No, I didn't consider that.
0
A =[1,78,46,4,34,10,50,2]
N = len(A)       # N = 8
def Algorithm(A,N):
    B = A[N]     # value of N is 8 and when you try to access A[8] its out of index range since the index is from 0 to 7
    B=[0]*N
    for i in range(1,N):
        B[A[i]]+=1
        i=1
    #for i <-- 1 to N
    for j in range(1,N):
    #for k <-- to B[j]
        for k in range(0,B[j]):
            A[i]=j
            i+=1
    return

Algorithm(A,N)
print(A)

I would like to point out other things that i have noted in your code.

A =[1,78,46,4,34,10,50,2]
N = len(A)       
def Algorithm(A,N):
    B = A[N]     
    B=[0]*N  # This line overwrites the above assignment. 
    for i in range(1,N):
        B[A[i]]+=1    # B[A[i]]  -- so i guess during execution the A[i] will be a value from list A . hence B[A[i]] might become B[78] , B[46] etc (sorry if i have misunderstood)
        i=1
    #for i <-- 1 to N
    for j in range(1,N):
    #for k <-- to B[j]
        for k in range(0,B[j]):
            A[i]=j
            i+=1
    return

Algorithm(A,N)
print(A)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.