List() function behaviour


Does list make a shallow copy or deep copy in Python 3? I wrote the following code to see:

X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Y = list(X)  # Makes a shallow copy
X.append([3445])
print(Y)
print(X)
X[0][1]="asddasdsaa"
print(Y)
print(X)

The output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [3445]]
[[1, 'asddasdsaa', 3], [4, 5, 6], [7, 8, 9]]
[[1, 'asddasdsaa', 3], [4, 5, 6], [7, 8, 9], [3445]]

Its seems that list is a shallow copy.

Same code with a simple list:

X = [1,23,22]
Y = list(X) # Seems like deep copy
X.append(334)
print(Y)
print(X)
X[0]=222
print(Y)
print(X)

The output:

[1, 23, 22]
[1, 23, 22, 334]
[1, 23, 22]
[222, 23, 22, 334]

This seems like a deep copy. I am confused.


Answer

@user2357112 is right, really, what part makes you thing that is a deep copy?

Actually @jonrsharpe is also right.

all copy ways, see below, are shallow copies:

l=l.copy()
l=l[:]
l=list(l)
...

Only copy.deepcopy is a deep copy:

from copy import deepcopy
l=deepcopy(l)