The code raises an AttributeError when run.
letters = ['a', 'b', 'c', 'a']
for letter in letters:
letter.remove('a')
print(letter)
The code raises an AttributeError when run.
letters = ['a', 'b', 'c', 'a']
for letter in letters:
letter.remove('a')
print(letter)
You have to use the remove() method on the list, not on its items. You should therefore use list.remove(item), without any for loops:
letters=['a', 'b', 'c', 'a']
letters.remove('a')
In reaction to your comment, here is a script to remove all occurences in a list:
while 'a' in letters:
letters.remove('a')
The following code is more like your code. I think that this is what you intended to do.
for letter in letters.copy():
if letter=='a':
letters.remove('a')
Note: Due to how for loops work in python, we need to make a temporary copy of the list. Otherwise certain items will be skipped.