On Udemy there was this challenge where you have to remove a specific string from the list. In this challenge, it was "spam". So here was the given List:
[
["egg", "bacon"],
["egg", "sausage", "bacon"],
["egg", "spam"],
["egg", "bacon", "spam"],
["egg", "bacon", "sausage", "spam"],
["spam", "bacon", "sausage", "spam"],
["spam", "sausage", "spam", "bacon", "spam", "tomato", "spam"],
["spam", "egg", "spam", "spam", "bacon", "spam"],
]
Below is the solution proposed by me:
menu = [
["egg", "bacon"],
["egg", "sausage", "bacon"],
["egg", "spam"],
["egg", "bacon", "spam"],
["egg", "bacon", "sausage", "spam"],
["spam", "bacon", "sausage", "spam"],
["spam", "sausage", "spam", "bacon", "spam", "tomato", "spam"],
["spam", "egg", "spam", "spam", "bacon", "spam"],
]
for meal in menu:
for item in meal:
if "spam" in item:
meal.remove("spam")
print(meal)
After the code is executed all "spam" is removed except the last one. I tried debugging and the removal of the last spam does get executed but not sure why it still shows in the output. Below is the output:
['egg', 'bacon']
['egg', 'sausage', 'bacon']
['egg']
['egg', 'bacon']
['egg', 'bacon', 'sausage']
['bacon', 'sausage']
['sausage', 'bacon', 'tomato']
['egg', 'bacon', '**spam**']
Can someone please help me to find the error? Or is this a bug?