Here are two pieces of similar codes:
s='abc'
ans,res=[],[]
for i in range(len(s)):
ans.append(i)
res.append(ans[:])
print(res,"
")
Result: [[0], [0, 1], [0, 1, 2]]
ans,res=[],[]
for i in range(len(s)):
ans.append(i)
res.append(ans)
print("
",res)
Result: [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
I am confused about the difference between the two results. Why do res.append(ans)
and res.append(ans[:])
lead to different return value?
Could anyone give me some hints or tell me any underlying logic that I ignored?