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’s have a look at a simple program, which demonstrates the use of the copy method in our program.
Python list copy
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’s have a look at the output of the above program –
[23, 12, 45, 29, 15, 31]
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’s try to make some changes to the copy list now. Have a look at the below program, which tries to demonstrate the same thing.
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’s have a look at the output now –
Copy list: [23, 12, 22, 29, 15, 31]
Original list: [23, 12, 45, 29, 15, 31]
As you can see, we had made some changes to the copy list, and the original list was not modified. Let’s 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’s have a look –
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 –
Copy list: [23, 12, 45, 29, 15, 31]
Original list: [23, 12, 45, 29, 15, 31]
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.
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’s have a look at the output now –
Copy list: [23, 12, 22, 29, 15, 31]
Original list: [23, 12, 22, 29, 15, 31]
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.