# remove all occurrences of a character in a list python

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

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

7 views
Problem: How to remove all occurrences of a value from a list?
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?
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
9 views
Problem: Please offer me an answer to the problem >Which method is used to remove all the elements from a list?
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.
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.
1 vote
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.