In Python, it is common to copy a list. With replication, there are naturally deep-copy and shallow-copy issues. The difference between deep copy and shallow copy is that when a new list is copied from the original list, whether the modification of either list will affect the other list, that is, whether the two lists are stored in the same area in memory, which is also an important basis for differentiating deep copy from shallow copy. Let’s look at several ways to copy a list in Python and see if it’s a deep copy or a shallow copy. Understanding this problem can help us avoid errors in programming and reduce unnecessary debugging time.
Non-copy method – direct assignment
If = is assigned directly, it is a non-copy method. The two lists are equivalent, and modifying either list affects the other. This is where Python, as a dynamic language, differs in thought from static languages like C.
1 #! /usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = old 6 print('Before:') 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print('After:') 12 print(old) 13 print(new)Copy the code
Running results:
Two, several methods of shallow copy
1. The copy () method
Let’s look at the following code:
1 #! /usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = old.copy() 6 print('Before:') 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print('After:') 12 print(old) 13 print(new)Copy the code
Running results:
For the first layer of a list, deep copy is implemented, but for nested lists, shallow copy is still implemented. This is actually pretty straightforward, because the inner list holds the address, and when you copy it over, you copy the address over. Nested lists still point to the same thing in memory.
2. Use list generators
Creating a new list using list generation is also a shallow copy method, with deep copy only for the first layer.
1 #! /usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = [i for i in old] 6 print('Before:') 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print('After:') 12 print(old) 13 print(new)Copy the code
Running results:
3. Loop through with the for loop
The elements are added to the new list one by one through the for loop. This is also a shallow copy method, implementing deep copies only for the first layer.
1 #! /usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = [] 6 for i in range(len(old)): 7 new.append(old[i]) 8 print('Before:') 9 print(old) 10 print(new) 11 new[0] = 3 12 new[1][0] = 3 13 print('After:') 14 print(old) 15 print(new)Copy the code
Running results:
4. Use slices
You can shallow copy the entire list by using [:] slices. Again, only deep copy is implemented for the first layer.
1 #! /usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = old[:] 6 print('Before:') 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print('After:') 12 print(old) 13 print(new)Copy the code
Running results:
Third, the implementation of deep copy
Using the deepCopy () method is the safest, cleanest and most efficient way to get a new list that is independent of the original, regardless of the number of layers and form.
To use, import copy.
1 #! /usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 import copy 5 6 old = [1, [1, 2, 3], 3] 7 new = copy.deepcopy(old) 8 print('Before:') 9 print(old) 10 print(new) 11 new[0] = 3 12 new[1][0] = 3 13 print('After:') 14 print(old) 15 print(new)Copy the code
Running results:
Above, is the list of replication and deep copy and shallow copy exploration. In view of my limited level, the article is not appropriate, please also point out in the comments.