-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Creating Deep Copies of Collections | ||
|
||
Python has a [built-in `copy` module](https://docs.python.org/3/library/copy.html#module-copy): | ||
|
||
> "Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module provides generic shallow and deep copy operations." | ||
This binding is especially useful for solving in-place modification of lists within a user-defined function: | ||
|
||
```python | ||
def rotate_list_plus_one_index(list): | ||
list.insert(0, list.pop()) | ||
|
||
nums = [1, 2, 3, 4, 5] | ||
rotate_list_plus_one_index(nums) | ||
print(nums) | ||
# [5, 1, 2, 3, 4] | ||
``` | ||
|
||
However, if we want to recursively create new copies of all the nested elements contained within a mutable object (vs. modifying in place), we can use the `deepcopy` method: | ||
|
||
```python | ||
import copy | ||
|
||
nums_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] | ||
nums_2d_deepcopy = copy.deepcopy(nums_2d) | ||
nums_2d_deepcopy[0][0] = 0 | ||
|
||
print(nums_2d) | ||
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]] | ||
|
||
print(nums_2d_deepcopy) | ||
# [[0, 2, 3], [4, 5, 6], [7, 8, 9]] | ||
``` |