Skip to content

Instantly share code, notes, and snippets.

@raytroop
Last active September 4, 2018 15:32
Show Gist options
  • Save raytroop/1d007b9a4f7985e6ce146b7dbfdff40a to your computer and use it in GitHub Desktop.
Save raytroop/1d007b9a4f7985e6ce146b7dbfdff40a to your computer and use it in GitHub Desktop.
python deepcopy
In [15]: import copy

In [23]: ax = [[1, 2], [3, 4]]

# deepcopy
In [24]: bx = copy.deepcopy(ax)

In [25]: ax
Out[25]: [[1, 2], [3, 4]]

In [26]: bx
Out[26]: [[1, 2], [3, 4]]

In [27]: ax[0][0] = 911

In [28]: ax
Out[28]: [[911, 2], [3, 4]]

In [29]: bx
Out[29]: [[1, 2], [3, 4]]

In [30]: ax = [[1, 2], [3, 4]]

# shallow copy
In [31]: bx = copy.copy(ax)

In [32]: ax
Out[32]: [[1, 2], [3, 4]]

In [33]: bx
Out[33]: [[1, 2], [3, 4]]

In [34]: ax[0][0] = 911

In [35]: ax
Out[35]: [[911, 2], [3, 4]]

In [36]: bx
Out[36]: [[911, 2], [3, 4]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment