Created
June 19, 2026 16:36
-
-
Save vanbasten23/0c54eaa55a3b99c042f8b6997de73d1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ``` | |
| x = [1, 2] | |
| a = [] | |
| a.append(x) | |
| x.append(3) | |
| print(a) | |
| # [[1, 2, 3]] | |
| ``` | |
| `append(x)` stores a reference to the same list object. When `x` changes later, the object inside a changes too. | |
| You can see it with `id()`: | |
| ``` | |
| x = [1, 2] | |
| a = [] | |
| a.append(x) | |
| print(id(x)) | |
| print(id(a[0])) | |
| # same number | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment