-1

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?

Boopiester
  • 141
  • 1
  • 10

2 Answers2

2

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]
0

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]
Shahad Mahmud
  • 410
  • 1
  • 4
  • 14