• Register
0 votes
50 views

Problem:

I am a newbee in python programming. I tried to remove all occurrences of a character in a list python. So I wrote a code:
Nums = [11, 22, 11, 33, 11, 44, 11, 55]
a = 11
print ("Original Numbers:")
print (Nums)
i=0 
length = len(Nums)   
while(i<length):
	if(Nums[i]==a):
		Nums.remove (Nums[i])
		length = length   
		continue
	i = i+1
print ("list after removing Numbers:")
print (Nums)

Which gives me following error:

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

 

Why I am getting this output but not the actual result. Please help

10 7 2
6,060 points

Please log in or register to answer this question.

2 Answers

0 votes

Answer:

In your program you have some logical problem.You are getting error beacuse of

length = length  

this line. You should have written this line like:

length = length -1  

And your code would be like:

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

If you use this code you should get an output like:

Original Numbers:
[11, 22, 11, 33, 11, 44, 11, 55]
list after removing Numbers:
[22, 33, 44, 55]

Happy Coding.

11 6 4
34,950 points
0 votes

Given a list, and we have to remove all occurrences of a given element from the list in python.

Example:

For example, the user enters the following input;

  list = [10, 20, 10, 30, 10, 40, 10, 50]

    n = 10

and we want to display the following output;

list after removing the 10 from the list;

list = [20, 30, 40, 50]

 Code for example:

# list with integer elements

list = [10, 20, 10, 30, 10, 40, 10, 50]

# number (n) to be removed

n = 10

# print original list 

print ("Original list:")

print (list)

# loop to traverse each element in list

# and, remove elements 

# which are equals to n

i=0 #loop counter

length = len(list) #list length 

while(i<length):

     if(list[i]==n):

           list.remove (list[i])

           # as an element is removed    

           # so decrease the length by 1 

           length = length -1  

           # run loop again to check element                                         

           # at same index, when item removed 

           # next item will shift to the left 

           continue

     i = i+1



# print list after removing given element

print ("list after removing elements:")

print (list)

Logic:

  • Run a while loop from the 0th element to the last element index.
  • Check the element whether it is equal to the number or not, which we want to remove.
  • If any element of the list is equal to the number, which is to be removed. Then remove that element from the list.
  • To remove the number from the list, use the list.remove() method.
  • After removing the element from the list, decrease the length, because one item is deleted, then continue the loop to check the next item at the same index (after removing the element, next element will be shifted to the previous index).
  • If the element not found, then increase the loop counter to check the next element.

 

I hope this will help to solve your problem!

11 5 2
3,890 points

Related questions

0 votes
1 answer 7 views
7 views
Problem: How to remove all occurrences of a value from a list?
asked Apr 16 sumaiya simi 43.9k points
0 votes
1 answer 75 views
75 views
Problem : How to remove all occurrences of a value from a list? I tried in Python remove() will remove the first occurrence of value in a list. How to remove all occurences?
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 40 views
40 views
Problem: I was trying to remove all occurrences of a character in a string python but I failed. The code I wrote is: def DeletChar(e, b) : counts = e.count(b) e = list(e) while counts : e.remove(b) counts -= 1 print(e) if __name__ == '__main__' : e = "geeksforgeeks" DeletChar(e,'g') The output ... e', 'k', 's', 'f', 'o', 'r', 'e', 'e', 'k', 's'] how to solve this problem I am a newbee please help
asked Mar 22, 2020 LizzyM 6.1k points
0 votes
1 answer 9 views
9 views
Problem: Please offer me an answer to the problem >Which method is used to remove all the elements from a list?
asked 5 days ago Danyal Sjoerd 6.8k points
0 votes
1 answer 25 views
25 views
Problem: The question is to traverse given string, as soon as we find an occurrence of we keep traversing until we find a character which is not c. We also set a flag to indicate that one more occurrences of c are seen. If we see c again and flag is set, then we return false.
asked Nov 2, 2020 Mashhoodch 13k points
0 votes
1 answer 23 views
23 views
Problem: have a list of lists in Python: k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]] And I want to remove duplicate elements from it. Was if it a normal list not of lists I could used set. But unfortunate that list is not hashable and can't ... [3], [4]] I don't care about preserve order. Note: this question is similar but not quite what I need. Searched SO but didn't find exact duplicate.
asked Feb 19 Mashhoodch 13k points
1 vote
1 answer 31 views
31 views
Problem: Hello Kodlogs, I am a newbie in Python programming. I am learning Python by enrolling in an online course. I am solving entry-level problems from some websites. I was doing well unless I get a question that asked for count occurrences of a particular item in a ... light on this? Please, help me to solve the problem. I am looking forward to having your answers. Thanks a lot in advance.
asked Jul 31, 2020 adamSw 11.3k points
0 votes
1 answer 6 views
6 views
Problem: Any body who knows that how to remove all instances of an element? The remove method removes all occurrences of the item from a list.
asked Apr 18 PkGuy 23.5k points
0 votes
1 answer 3 views
3 views
Problem: PLease help me writing a python program to remove duplicates from a list. Thank you.
asked 2 days ago Junia Phoebe 15.3k points
0 votes
1 answer 8 views
8 views
Problem: how to I Solve this: remove a list from a list python HELP! I need help understanding this: remove a list from a list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points