I was just trying to shorten a code snippet in python and came across the strange way of working of the following code.
sample = [[]] * 3
# OUTPUT - [[], [], []] - Simple enough to understand
sample[0].append(1)
# OUTPUT - [[1],[1],[1]] - Why is this happening?
sample[1].append(2)
# OUTPUT - [[1,2],[1,2],[1,2]] - Why is this happening?
sample[2].append(3)
# OUTPUT - [[1,2,3],[1,2,3],[1,2,3]] - Why is this happening?
Why is appending to a nested list appending to all the nested lists in the list?
Are all the nested lists pointing to one list? If so where is the original copy of the list?