x = [1,2,3]
y = x
y.append(4)
print("y after append", y)
# x and y are ALIASED -- so when we append to y,
# we also change x!
print("x after append", x)
###
# lst is an alias for the argument, so when we
# DESTRUCTIVELY modify lst, the changes are seen
# outside the function call.
def destructiveDouble(lst):
for i in range(len(lst)):
lst[i] = lst[i] * 2
x = [1,2,3]
print("x before destructiveDouble", x)
y = destructiveDouble(x)
print("x and y after destructiveDouble", x, y)
###
# Here, we don't destructively change lst, so
# it stays the same. Instead, we return the
# newly-calculated values in a new list.
def nonDestructiveDouble(lst):
result = []
for i in range(len(lst)):
result.append(2 * lst[i])
return result
x = [1,2,3]
print("x before nonDestructiveDouble", x)
y = nonDestructiveDouble(x)
print("x and y after nonDestructiveDouble", x, y)