3
import copy

original = [[1]]

a = original[:]
b = list(original)
c = copy.copy(original)

original[0][0] = 2

print(a) # [[2]]
print(b) # [[2]]
print(c) # [[2]]

Prints the same thing, so is there any difference?
Or it's different, but in other cases,
if that's the case please provide not only explanations, but snippets too.

This question is not a duplicate of this question,
because this questions has nothing to do with deep copy.
But for some reason it marked as duplicated exactly for that.

Community
  • 1
  • 1
Parki
  • 159
  • 9
  • 2
    Each of the three options creates a *shallow copy*, so the nested list is *not* copied, it is still shared between the three copies of the outer original list. See the duplicate. – Martijn Pieters Mar 12 '17 at 06:15
  • 1
    Possible duplicate of [How to clone or copy a list](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Keerthana Prabhakaran Mar 12 '17 at 06:16
  • The `original` list consists of one item: a reference to the list `[1]`. All three methods create a shallow copy of your list: `a`, `b`, and `c` are three different objects, but each consists of one item, a reference to the same list `[1]`. When you modify `original[0][0]`, all three copies "feel" the change. – DYZ Mar 12 '17 at 06:16

0 Answers0