• Register
1 vote
25 views

Problem:

I am new in python programming and I have been following a course where I have been instructed to do an assginement to remove all occurrences of a character in a list python. So I write this code:

Numbs = [11, 22, 11, 33, 11, 44, 11, 55]
a = 11
print ("Original Numbers:")
print (Numbs)
i=0 
length = len(Numbs)   
while(i<length):
	if(Numbs[i]==a):
		Numbs.remove (Numbs[i])
		length = length   
		continue
	i = i+1
print ("list after removing Numbers:")
print (Numbs)

but when I run this thing it compiles but sohws:

Original Numbers:
[11, 22, 11, 33, 11, 44, 11, 55]
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    if(Numbs[i]==a):
IndexError: list index out of range

Can anyone help me out to solve this problem?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Answer:

In your code the error message is appearing beacuse of the line
length = length  

You should have written this line like:

length = length -1  

And your code would be like:

Numbs = [11, 22, 11, 33, 11, 44, 11, 55]
a = 11
print ("Original Numbers:")
print (Numbs)
i=0 
length = len(Numbs)   
while(i<length):
	if(Numbs[i]==a):
		Numbs.remove (Numbs[i])
		length = length - 1   
		continue
	i = i+1
print ("list after removing Numbers:")
print (Numbs)

 Hope this may help you. Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 8 views
8 views
Problem Looking for guidance to handle indexerror list index out of range in python.
asked Apr 5 salim1 60.6k points
1 vote
1 answer 7 views
7 views
Problem: How to fix this error : python indexerror list index out of range
asked Mar 20 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 6 views
6 views
Problem: Any ideas on what I need to do to fix this? python indexerror: list index out of range
asked Mar 20 MUHAMMAD MUNEEB 83k points
1 vote
2 answers 138 views
138 views
Problem: I am trying to adopt the python programming language. Learning it step by step and practicing daily. I&rsquo;ve written simple code that contains a list and run an iteration on it but it shows: python indexerror: list assignment index out of range m = [1, 2, 3, 5, 8, 13] n = [] o = 0 for l in m: n[o] = l o += 1 print n Where did I wrong? Any Idea?
asked Mar 30, 2020 Gavin 15.3k points
0 votes
1 answer 45 views
45 views
I wrote a code in python : l = [8, 5, 4] total = l[0] + l[1] print (total) def count(a): total = 0 for i in a: total+= a[i] return total print (count(n)) but it has some error: File "main.py", line 11 return total ^ SyntaxError: 'return' outside function What is this error causing for?
asked Mar 15, 2020 LizzyM 6.1k points
0 votes
1 answer 7 views
7 views
Problem: Why do I get this error: "Pop index out of range?"
asked Apr 17 sumaiya simi 43.9k points
0 votes
1 answer 13 views
13 views
Problem: I am getting an error stating that the pop index of g in indexList is out of range: IndexError: pop index out of range
asked Apr 9 ummesalma 29.2k points
1 vote
1 answer 34 views
34 views
Problem: I was discovering String in python. Suddenly I stuck here by getting &ldquo;indexerror: string index out of range&rdquo; this error message. def print_letters(string): count = 0 newstring = [] while count <= len(string): m = string[count] count +=1 print m print_letters("blanks") Do you have any idea? I&rsquo;d appreciate it if you figure the error in my program.
asked Mar 7, 2020 Gavin 15.3k points
1 vote
1 answer 58 views
58 views
Problem: &ldquo;indexerror: tuple index out of range&rdquo; what does it mean? Why I am getting this error? Please shed some light on this if possible. Gratitude.
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 4 views
4 views
Problem: Index error list index out of range.
asked Apr 7 charles mathews 5.5k points