I know that a list L can be copied by doing L[:]. But I face an issue that I do not understand why.
src = [1,2,3]
dest = [[5,6,7]]
dest.append(src[:].append(4))
dest
[[5, 6, 7], None]
In the above sample, the src list is not copied to dest (see None) when I tried to copy and append 4 to it.
dest.append(src[:])
dest
[[5, 6, 7], None, [1, 2, 3]]
As seen in the above snippet, if I add simply add the list (to dest) without any append attempt, it gets inserted.
Any idea?
What
appendtries to do is to append data on the given list and returnsNone.That’s why you will see
Noneat the end ofdest.The following code should do what you want: