Although both functions are logically doing the same thing i.e. doubling the list by itself, why does one function modify the original list and the other doesn't?
The only difference visible in the code is that func2()
uses short-hand notation for doubling while func1()
doesn't.
Aren't my_list *= 2
and my_list = my_list * 2
equivalent?
Approach 1:
def func1(my_list):
my_list = my_list * 2
x = [1, 2, 3]
func1(x)
print(x)
Output:
[1, 2, 3]
Approach 2:
def func2(my_list):
my_list *= 2
x = [1, 2, 3]
func2(x)
print(x)
Output:
[1, 2, 3, 1, 2, 3]