I have a question on the remove
method of lists in python when using for loop.
When I am not using the remove
method, the loop works as expected.
a_copy = a = [1, 2, 3, 4, 5]
print(a_copy)
b = ["a", "b", "c"]
for item in a:
if item not in b:
print(item)
Output:
[1, 2, 3, 4, 5]
1
2
3
4
5
However, when trying to remove elements using the same if statement in the for loop, the output was weird:
a_copy = a = [1, 2, 3, 4, 5]
print(a_copy)
b = ["a", "b", "c"]
for item in a:
if item not in b:
print(item)
a_copy.remove(item)
Output:
[1, 2, 3, 4, 5]
1
3
5
Anyone has an opinion on what's wrong here, please?