I'm trying to remove elements of a list if they can be found somewhere in a string.
But somehow, the final list contains elements that surely are not in the string.
Can someone point me in the right direction? Not sure if it's because the list is being altered during the loop or something like that.
Here's my code:
big_string = '209301293012931234847123'
small_strings = ['1', '2', 'sugar', 'juice', '666']
for element in small_strings:
if element not in big_string:
small_strings.remove(element)
print(small_strings)
The output I expected is:
['1', '2']
Instead what I get is:
['1', '2', 'juice']
Note: If I make a copy of the list and modify only that list (using the other one as reference) it seems to work perfectly. Why is that? Is there another way to do it, besides creating a copy?