• Register
0 votes
24 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

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 1
3,890 points

Related questions

0 votes
1 answer 31 views
31 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 19 views
19 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 LizzyM 6.1k points
1 vote
1 answer 22 views
22 views
Problem: Recently I started learning python and just about to know more about list and tuple in python. I am a little bit known to the list and tuple and I can add or subtracts elements in a list. I wonder how to multiply all elements in a list python? I tried it in the adding process but it didn&rsquo;t work for me. Is it doable? Could anybody please show me the way to do it?
asked Apr 3 Gavin 15.3k points
1 vote
1 answer 25 views
25 views
Problem: I have a list that contains the values: list = [2, 4, 8, 16, 32, 2, 64, 128, 2, 256, 512, 2] I wrote a python program for this where I could find out how many times a value appears in a list python: def count_appearence(list, z): check = ... count_appearence(list, z))) NameError: name 'z' is not defined What is the problem with my code. Please help me solve this thing. Thanks in advance.
asked May 13 samhaz 5k points
0 votes
2 answers 88 views
88 views
Problem : Python Program to Calculate the Average of Numbers in a Given List
asked Oct 21, 2019 peterlaw 6.9k points