I am a relative python novice. I have a simple function here that takes in a list and removes an element of the list. However, I noticed that the function actually alters the list outside of the function. For example,
def test(lista):
lista.remove(1)
return(lista)
def main():
a = [1,2,3]
print(a)
x = test(lista=a)
print(a)
It turns out that the first call to print(a)
, I get [1, 2, 3]
as expected, but the second call to print(a)
, I get [2, 3]
which doesn't quite make sense to me because I'm not sure why the function test
would remove the element from a
. I understand that I pass a
in as a parameter, but I'm not sure why lista.remove(1)
would remove the element 1
from both a
and lista
.
Thanks!