{"id":3262,"date":"2022-11-14T19:43:34","date_gmt":"2022-11-14T19:43:34","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3262"},"modified":"2022-11-14T19:43:36","modified_gmt":"2022-11-14T19:43:36","slug":"python-list-copy","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-list-copy\/","title":{"rendered":"Python list copy() method"},"content":{"rendered":"<p class=\"LC20lb MBeuO DKV0Md\" style=\"text-align: center;\">Python list copy() method<\/p>\n<p>Now, we are going to learn about the copy method. As the name of the method says, this method is going to return us the shallow copy of the list. Let\u2019s have a look at a simple program, which demonstrates the use of the copy method in our program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3271 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON.jpg\" alt=\"\" width=\"903\" height=\"121\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON.jpg 903w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-300x40.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-768x103.jpg 768w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/><\/p>\n<h3 class=\"LC20lb MBeuO DKV0Md\" style=\"text-align: center;\">Python list copy<\/h3>\n<p>As you can see in the above program, we have a simple list, and then we are trying to use the copy method on the list. The copy method basically returns a shallow copy of the list, which is assigned to the variable copy_list in the above program. Let\u2019s have a look at the output of the above program &#8211;<\/p>\n<p>[23, 12, 45, 29, 15, 31]<\/p>\n<p>As you can see, we have got a copy of the list. Remember that the method returns a new list and does not modify the original list. Now, the thing is that if you modify this particular list, this will not affect the original list. Let\u2019s try to make some changes to the copy list now. Have a look at the below program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3272 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-1.jpg\" alt=\"\" width=\"986\" height=\"154\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-1.jpg 986w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-1-300x47.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-1-768x120.jpg 768w\" sizes=\"auto, (max-width: 986px) 100vw, 986px\" \/><\/p>\n<p>As you can see in the above program, we are trying to make some changes to the copy list, and this does not modify the original list. We have printed both lists, after modifying the copy list, and we can find that the original list is not modified. Let\u2019s have a look at the output now &#8211;<\/p>\n<p>Copy list: [23, 12, 22, 29, 15, 31]<\/p>\n<p>Original list: [23, 12, 45, 29, 15, 31]<\/p>\n<p>As you can see, we had made some changes to the copy list, and the original list was not modified. Let\u2019s try one more thing, where we will try to use the simple assignment operator and see if we can make a copy of the list or not. Let\u2019s have a look &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3273 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-2.jpg\" alt=\"\" width=\"1064\" height=\"136\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-2.jpg 1064w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-2-300x38.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-2-1024x131.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-2-768x98.jpg 768w\" sizes=\"auto, (max-width: 1064px) 100vw, 1064px\" \/><\/p>\n<p>As you can see, in the above program, we are having a list, and then we are trying to make a copy of list just by using the simple assignment operator. Here as well, if you look at the output, you would see something like this &#8211;<\/p>\n<p>Copy list: [23, 12, 45, 29, 15, 31]<\/p>\n<p>Original list: [23, 12, 45, 29, 15, 31]<\/p>\n<p>As you can see, it seems that we got a copy of the list, but it is not true. If we are making use of the simple assignment operator, then the copy_list reference variable now points to the same list, as the myList variable. So, this is not the copy, but now both the variables are pointing to the same list. So, any modifications done with the copy_list variable will affect the same list. Have a look at the below program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3274 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-3.jpg\" alt=\"\" width=\"934\" height=\"186\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-3.jpg 934w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-3-300x60.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-copy-METHOD-IN-PYTHON-3-768x153.jpg 768w\" sizes=\"auto, (max-width: 934px) 100vw, 934px\" \/><\/p>\n<p>As you can see, we have a list, and then we are trying to make a copy of the list, using the simple assignment operator. Then, we are trying to make changes to the list, whose reference variable is copy_list. But since both the reference variables are pointing to the same list, the operation affects the same list. Let\u2019s have a look at the output now &#8211;<\/p>\n<p>Copy list: [23, 12, 22, 29, 15, 31]<\/p>\n<p>Original list: [23, 12, 22, 29, 15, 31]<\/p>\n<p>As you can see, we got the changes here, affecting the same list, since there are not two different lists, but two reference variables, which are pointing to the same list. So, we must be careful when we are doing this since doing some changes can affect our original list. Whenever we need a copy of our list, we can make use of the copy method. Alternatively, we can also make use of list slicing, to get a copy of the list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python list copy() method Now, we are going to learn about the copy method. As the name of the method says, this method is going to return us the shallow copy of the list. Let\u2019s have a look at a simple program, which demonstrates the use of the copy method in our program. Python list [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3262","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3262"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3262\/revisions"}],"predecessor-version":[{"id":4077,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3262\/revisions\/4077"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}