In this Python tutorial, we will discuss the Python List copy() method. It is an essential method of the list class that allows us to create a copy of a given Python list.
List copy() method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to Python List copy() method
- Syntax of the copy() method
- Python Shallow Copy vs Python Deep Copy
- Example of the copy() method
Python List copy() method
The list copy() method creates a shallow copy of a given Python list. A shallow copy means that the new list will have its own reference in memory but will share references for mutable elements, like nested lists.
The syntax for the copy() method is as follows:
new_list = old_list.copy()
Python Shallow Copy vs Deep Copy
The following is the difference between the Shallow Copy and Deep Copy in Python.
Python Shallow Copy | Python Deep Copy |
---|---|
In Python, shallow copy creates a new list, but it does not create new objects for the mutable elements within the Python list. | In Python, deep copy creates a new Python list and also creates new objects for all the mutable elements within the Python list. |
It shares the references of the original Python list’s mutable elements. | It ensures that changes made to the original Python list do not affect the new Python list, and vice versa. |
For this, Python has a built-in copy() function. | For this, we have to import the copy module first, then we are able to use the deepcopy() method. |
copy() method in Python List Examples
Let’s dive into some examples to see the copy() method in action.
Example#1 Creating a Shallow Copy
original_states = ['California', 'New York', 'Texas', 'Florida', 'Illinois']
copied_states = original_states.copy()
print("Original States:", original_states)
print("Copied States:", copied_states)
In this example, we have an original Python list original_states
containing five US state names. We then create a new Python list copied_states
using the copy() method.
When we print both Python lists, we can see that they contain the same elements, indicating that the Python copy() method has successfully created a shallow copy of the original list.
Output:
Example#2 Demonstrating Shared References in Shallow Copy
original_cities = ['Los Angeles', 'New York', ['Houston', 'Dallas'], 'Miami']
copied_cities = original_cities.copy()
original_cities[2][0] = 'Austin'
print("Original Cities:", original_cities)
print("Copied Cities:", copied_cities)
In this example, we have an original Python list original_cities
containing US city names, including a nested Python list with two city names. We create a new Python list copied_cities
using the copy() method.
Then, we modify the original Python list by changing the first city in the nested list from ‘Houston’ to ‘Austin’. When we print both Python lists, we can see that the change in the original list is also reflected in the copied list.
This is because the Python list copy() method creates a shallow copy that shares references to mutable elements, such as nested Python lists. As a result, changes made to mutable elements in the original list will also affect the copied list.
Output:
Conclusion
In this article, we covered the Python list copy() method, its usage, and its importance when working with lists. We also discussed the difference between shallow and deep copies and demonstrated the behavior of the copy() method using examples.
You may also like to read the following articles:
- Python List clear() method [With Examples]
- Python merge two lists without duplicates
- How to concatenate all elements of list into string in Python
- Python program to sort list of tuples
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.