A study of copy and deep copy and shallow copy of list in Python

Source: Internet
Author: User
Tags shallow copy

In Python, it is common to copy a list. For replication, there is a natural deep copy and a shallow copy problem. The difference between a deep copy and a shallow copy is that when a new list is copied from the original list, modifying any one of them will affect the other, that is, whether the two lists are stored in the same area in memory, which is an important basis for distinguishing between deep and shallow copies. Next, let's look at some of the ways in which Python lists are copied, whether it's a deep copy or a shallow copy. Figuring this out helps us avoid errors in programming and reduce unnecessary debugging time.

First, non-copy method-Direct assignment

If you use = Direct assignment, the non-copy method. These two lists are equivalent, and modifying any of these lists will affect the other list. This is also the difference between the thought of Python as a static language such as dynamic language and C.

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4Old = [1, [1, 2, 3], 3]5New = Old6 Print('Before:')7 Print(old)8 Print(new)9New[0] = 3TenNew[1][0] = 3 One Print('After :') A Print(old) - Print(new)

Operation Result:

Several methods of secondary and shallow copy

1.copy () Method

Let's look at the following code:

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4Old = [1, [1, 2, 3], 3]5New =old.copy ()6 Print('Before:')7 Print(old)8 Print(new)9New[0] = 3TenNew[1][0] = 3 One Print('After :') A Print(old) - Print(new)

Operation Result:

For the first layer of the list, a deep copy is implemented, but for nested lists, it is still a shallow copy. This is actually very good understanding, the inner layer of the list is to save the address, copy the past is to copy the address has passed. The nested list is pointing to the same in memory.

2. using list-generated

Using list generation to generate a new list is also a shallow copy method that only deep copies are implemented on the first layer.

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4Old = [1, [1, 2, 3], 3]5new = [I forIinchOld ]6 Print('Before:')7 Print(old)8 Print(new)9New[0] = 3TenNew[1][0] = 3 One Print('After :') A Print(old) - Print(new)

Operation Result:

3. traverse with for Loop

With a For loop traversal, elements are added to the new list. This is also a shallow copy method, which only enables deep copies of the first layer.

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4Old = [1, [1, 2, 3], 3]5New = []6  forIinchRange (len (old)):7 new.append (Old[i])8 Print('Before:')9 Print(old)Ten Print(new) OneNew[0] = 3 ANew[1][0] = 3 - Print('After :') - Print(old) the Print(new)

Operation Result:

4. using slices

By using the [:] slice, you can simply copy the entire list. Similarly, deep copies are only implemented on the first layer.

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4Old = [1, [1, 2, 3], 3]5New =old[:]6 Print('Before:')7 Print(old)8 Print(new)9New[0] = 3TenNew[1][0] = 3 One Print('After :') A Print(old) - Print(new)

Operation Result:

Third, deep copy implementation

If you use the Deepcopy () method, no matter how many layers, regardless of the form, the resulting new list is unrelated to the original, this is the safest and most refreshing the most effective method.

When you use, you import copy.

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4 ImportCopy5 6Old = [1, [1, 2, 3], 3]7New =copy.deepcopy (old)8 Print('Before:')9 Print(old)Ten Print(new) OneNew[0] = 3 ANew[1][0] = 3 - Print('After :') - Print(old) the Print(new)

Operation Result:

  The above, is the copy of the list and deep copy and shallow copy exploration. In view of my limited level, there are irregularities in the text, please also indicate in the comments.

A study of copy and deep copy and shallow copy of list in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.