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, 2019 in Python by Alok
6,154 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 to solve this IndexError: list index out of range in python and streamlit?

Hi,  @Erick, To solve the “index error: list ...READ MORE

answered 4 days ago in Python by Roshni
• 3,870 points
69 views
0 votes
0 answers

Size is (121,512,512). Error is coming as : IndexError: index 152 is out of bounds for axis 0 with size 121.

def nodule_detect(nodules,j,numpyImage, numpyOrigin, numpySpacing): ...READ MORE

Jan 28 in Python by anonymous
• 150 points
1,798 views
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,780 points
311 views
–1 vote
2 answers
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, 2019 in Python by Junaid
155 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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,365 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,011 views