Python error "IndexError: list index out of range"

0 votes

I am trying to execute the following python code:

def construct(s, k, a):
    index = 0
    # Finding the index which is not -1
    for i in range(s):
        if (a[i]!=-1):
            index = i
            break
    # Calculating the values of the indexes index-1 to 0
    for i in range(index-1, -1, -1):
        if (a[i]==-1):
            a[i]=(a[i + 1]-1 + k)% k
    # Calculating the values of the indexes index + 1 to n
    for i in range(index + 1, s):
        if(a[i]==-1):
            a[i]=(a[i-1]+1)% k
            print(a)
# Driver code
s, k = 6, 7
a = [1, 2, 3, 4, 5]
construct(s, k, a)

I get the following error:

IndexError: list index out of range
Jun 17 in Python by Alok
728 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
74 views
0 votes
1 answer

Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

answered May 14 in Python by Junaid
36 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
627 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
678 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 232 views
+4 votes
6 answers