I have this code:
def jaja(lista):
lista.append(2)
return lista
a=[2,3]
b=jaja(a)
print(a,b)
I was hoping to get [2,3] [2,3,2], but for some strange reason list a also changes, so I get [2,3,2] [2,3,2]. Ideas??
I have this code:
def jaja(lista):
lista.append(2)
return lista
a=[2,3]
b=jaja(a)
print(a,b)
I was hoping to get [2,3] [2,3,2], but for some strange reason list a also changes, so I get [2,3,2] [2,3,2]. Ideas??
a
changes because the list gets passed by reference into your function, so when you append in the function, you're appending to the original list. If you don't want the original list to change, make a copy:
def jaja(lista):
lista = lista[:] # a simple way to copy a list in Python
lista.append(2)
return lista
a=[2,3]
b=jaja(a)
print(a,b) # prints [2,3] [2,3,2]