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.
Syntax of the Python List copy() Method
The following is the syntax of the Python list copy() method:
new_list = original_list.copy()
Parameter
- The copy() method does not take any parameters.
Return Value
- The copy() method returns a shallow copy of a list.
Examples of List copy()
We will now take a look at some examples of the list copy() method in Python.
Example 1: Working of the copy() Method
In the following example, we will understand the working of the copy() method to create a shallow copy of a given list.
Example
# Simple example of List copy() Method
# given list
car_list = ["Tata", "Honda", "Mahindra", "Toyota", "BMW"]
print("Given List:", car_list)
# creating a copy of a list using copy()
newcar_list = car_list.copy()
print("Copied List:", newcar_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.
Example 2: Modifying the Copied List
We will now look at an example to see what happens when we try modifying the copied list.
Example
# Simple example of List copy() Method
# given list
car_list = ["Tata", "Honda", "Mahindra", "Toyota", "BMW"]
# creating a copy of a list using copy()
newcar_list = car_list.copy()
# modifying the copied list
newcar_list.append("Audi")
# printing results
print("Given List:", car_list)
print("Modified List:", newcar_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.
Example 3: Copying a List of Lists
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.
Example
# Simple example of List copy() Method
# given list
matrix = [
[1, 3],
[5, 2]
]
# creating a copy of a list using copy()
new_matrix = matrix.copy()
# modifying the copied list
new_matrix[0][0] = 99
print("Given List:", matrix)
print("Modified List:", new_matrix)
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.
Example 4: Copying a List of Mixed Data Types
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.
Example
# Simple example of List copy() Method
# given list
mixed_list = [10, 'Hello', 2.4, [3, 6], {'python' : 'app'}]
# creating a copy of a list using copy()
new_mixed_list = mixed_list.copy()
# modifying the copied list
new_mixed_list[3].append(11)
# printing results
print("Given List:", mixed_list)
print("Modified List:", new_mixed_list)
Output:
Given List: [10, 'Hello', 2.4, [3, 6, 11], {'python': 'app'}]
Modified List: [10, 'Hello', 2.4, [3, 6, 11], {'python': 'app'}]
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].
Conclusion
Python contains different built-in methods and functions that help developers to easily create flawless software. The Python list copy() method is extremely useful in situations where we want to create a shallow copy. This tutorial has covered all the details and examples regarding this method, but don’t limit to these examples; instead, try more and more examples to be proficient.
Leave a Reply