I am running into a problem when passing a list to a function. It appears to have a global effect on the list variable, but I did not declare it as global in my function. Can anyone tell me what's happening and how to work around it?
def a_Minus_b(a,b):
for i in range(len(b)):
print("a= ", a)
if b[i] in a:
a.remove(b[i])
return a
x = [1,2,3,4]
a_Minus_b(x,x)
a= [1, 2, 3, 4]
a= [2, 3, 4]
a= [2, 4]
Error:
Traceback (most recent call last):
File "<pyshell#115>", line 1, in <module>
a_Minus_b(x,x)
File "<pyshell#112>", line 4, in a_Minus_b
if b[i] in a:
IndexError: list index out of range