tags:

views:

129

answers:

2
def myfunc(x):
 y = x
 y.append('How do I stop Python from modifying x here?')
 return y

x = []
z = myfunc(x)
print(x)
+9  A: 

You do:

y = x[:]

to make a copy of list x.

jcao219
It's worth noting that the copy is shallow.
FM
+1  A: 

You need to copy X before you modify it,

def myfunc(x):
 y = list(x)
 y.append('How do I stop Python from modifying x here?')
 return y

x = []
z = myfunc(x)
print(x)
Petriborg