# remove all occurrences of a character in a list python

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``````

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.

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!

## Related questions

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?
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
1 vote