In Python, the copy() method is a list method used to create a shallow copy of a list. The list copy() method creates a new list consisting of the same elements as the original list; however, it maintains its own identity in the memory. It is useful when we want to make sure that the changes to the new list do not affect the original list and vice versa.
The following is the syntax of the Python list copy() method:
We will now take a look at some examples of the list copy() method in Python.
In the following example, we will understand the working of the copy() method to create a shallow copy of a given list.
Output:
Given List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW'] Copied List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW']
Explanation:
In the above example, we are given a list. We used the copy() method to create a shallow copy of the given list and stored the generated list in a new variable, newcar_list.
As a result, we have successfully copied the content of the given list to a new list.
We will now look at an example to see what happens when we try modifying the copied list.
Output:
Given List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW'] Modified List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW', 'Audi']
Explanation:
In this example, we are given a list. We used the copy() method to copy the list and stored in a variable, newcar_list. We then used the append() method to add a new element to the copied list.
As a result, after appending 'Audi' to newcar_list, the original list remains unchanged. This shows that both the lists are independent.
Even though the copy() method allows us to create a new list; however, it is a shallow copy. Modifying the inner list in copied list also affects original list as both lists reference the same inner lists.
Output:
Given List: [[99, 3], [5, 2]] Modified List: [[99, 3], [5, 2]]
Explanation:
In the above example, we are given a list of lists. We used the copy() method to create a shallow copy of the given list. We then modified an element of the nested list of the copied list.
Although both the lists are separate lists, the nested lists inside them are still share the same reference. Therefore, modifying a nested item in the copied list also affects the original list. This is what makes it a shallow copy.
In the following example, we will create a shallow copy of a list consisting of multiple data types, such as numbers, strings, lists, and dictionaries.
Output:
Given List: [10, 'Hello', 2.4, [3, 6, 11], {'tpoint': 'tech'}]
Modified List: [10, 'Hello', 2.4, [3, 6, 11], {'tpoint': 'tech'}]
Explanation:
In the above example, we are given a list of mixed data types. We used the copy() method to create a shallow copy of the given list. We then modified the nested list of the copied list by appending a new element to it.
Although both the lists are different, the lists inside them are still share the same reference. So when 11 is appended to new_mixed_list[3], it also changes mixed_list[3].
We request you to subscribe our newsletter for upcoming updates.