Because of the mutable objects in the list, a and b actually only want the same address. Remove on b will affect the iteration of a. If you don’t believe me, print out a and see
a = ["asd_1", "asd_2", "3", "4"]
b = a
for i in a:
print(i)
if i.find('asd_') < 0:
b.remove(i)
print a
Because of the mutable objects in the list, a and b actually only want the same address. Remove on b will affect the iteration of a. If you don’t believe me, print out a and see
Output:
At this time, the length of a has become 3
In the above code, b is just a reference to a. If you modify b, a will also be modified, which directly affects the iteration of a.
You can try it
or