Copies and Shared Objects
This page continues from Object References and Identity. It covers what happens when you copy a list, the difference between a shallow and a deep copy, and the cases where Python makes variables share an object for you.
Shallow Copies vs. Deep Copies
We can make a copy of a list like this:
copied_list now holds a copy of original_list, but it's just a shallow copy. The outer list is a separate object:
But the elements are shared. The first element (the int) is the same object in both:
And so is the second element (the inner list):
We can see that although original_list and copied_list refer to different objects, the elements of original_list and the elements of copied_list refer to the same objects. If you mutate the list referenced by original_list[1], and then print copied_list:
You'll see that copied_list shows that change as well.
A deep copy, however, would be an entirely separate copy (no shared sub-elements or sub-sub-elements, etc.). If we had made a deep copy in this example instead of a shallow copy, it would look like this:
We could have achieved a deep copy by also making a copy of any sublists (or other mutable sub-elements). Here the inner lists end up as separate objects:
There is a Python module that can be used to make deep copies, but we won't cover that here.
Continuing with our shallow copy example, if you rebind original_list[0] and then print copied_list:
You'll see that copied_list hasn't changed. That's because we're not mutating the object referred to by original_list[0] (it's an int, and ints are immutable). Instead, we're rebinding original_list[0] to a new int object.
We were able to mutate the object referred to by original_list[1] because it's a list, and lists are mutable. If we wanted, we could rebind original_list[1] instead, and copied_list wouldn't be affected, because we're not mutating a shared list, but instead rebinding original_list[1] to a new list object:
Check your understanding. Predict the result. One of the names is an alias and one is a copy.
a: list[int] = [1, 2, 3]
b: list[int] = a
c: list[int] = list(a)
a.append(4)
Predict what each variable holds after the program finishes. The program is not run for you. Replace each None in the box below with your prediction, and give each variable a type hint, like name: type = value. Then press Run to find out if you are right. Write text in quotes, like "abc". Write a list in square brackets, like [1, 2, 3].
final_ais the value ofaat the endfinal_bis the value ofbat the endfinal_cis the value ofcat the end
Hint 1
For each name, ask whether it is another name for the same list, or a separate copy made at some point.
Automatic Sharing
Sometimes Python is able to figure out that it can save memory by automatically making variables refer to the same object.
This only ever happens with (equal) immutable values, so it can't affect your code's results.
A note about Python internals:
You might notice that this example using 5 gives a different result from the example that used 1000 on the previous page. It depends too on whether you're running the code in the interactive console or running it as a script. In the interactive console, Python internally keeps an array of integer objects for -5 through 256. When you create an int in that range, you actually just get back a reference to the existing object. So if you do that with two (or more) variables, they will then refer to the same object. However, if you type that code as a script in an IDE and then try running it with different values, you can see that any integer values will be shared, not just from -5 to 256. This is because when you're running a script, the Python interpreter can look through the entire script before running it, so it knows all the values that could be shared.
Explanation
An alias is the same list. Both copies are new outer lists.
Quizzes
- Predict the result. The copy is a shallow copy.
original: list[int | list[int]] = [1, [2, 3]]
copied: list[int | list[int]] = list(original)
copied[0] = 99
copied[1].append(4)
Predict what each variable holds after the program finishes. The program is not run for you. Change the values below to your predictions:
final_originalis the value oforiginalat the endfinal_copiedis the value ofcopiedat the end
Hint 1
A shallow copy makes a new outer list but reuses the lists inside it. Ask which parts of each list are shared.