I have two examples below. Why does Example 1 return duplicate values? I am expecting both code snippets to return
[[4], [4, 3], [4, 3, 2], [4, 3, 2, 1]]
Ex 1:
def Example1(arr):
listToInput = []
result = []
for i in range(len(arr) - 1, -1, -1):
listToInput.append(arr[i])
result.append(listToInput)
return result
Ex 2:
def Example2(arr):
listToInput = []
result = []
for i in range(len(arr) - 1, -1, -1):
listToInput.append(arr[i])
result.append(list(listToInput))
return result
print(Example1([1,2,3,4])) # Output is [[4, 3, 2, 1], [4, 3, 2, 1], [4, 3, 2, 1], [4, 3, 2, 1]]
print(Example2([1,2,3,4])) # Output is [[4], [4, 3], [4, 3, 2], [4, 3, 2, 1]]