In this example:
m = [100, 33, 234, 5]
n = m
m.append(89)
print(n)
# Output: [100, 33, 234, 5, 89]
How do I make it so n
doesn't contain 89?
In this example:
m = [100, 33, 234, 5]
n = m
m.append(89)
print(n)
# Output: [100, 33, 234, 5, 89]
How do I make it so n
doesn't contain 89?
The easiest way is [:]
, but note that this only applies to single-level arrays, if you need to deal with multi-level arrays (such as: [[1,2],[3,4]]
), you need to use deepcopy
m = [100, 33, 234, 5]
n = m[:]
m.append(89)
print(n)
# Output: [100, 33, 234, 5]
You have to copy the first list.
m = [100, 33, 234, 5]
n = m.copy()
m.append(89)
print(n)
# Output: [100, 33, 234, 5]