Python List copy() Method
Last Updated :
20 Dec, 2023
The list Copy() method makes a new shallow copy of a list.
Example
Python3
fruits = [ "mango" , "apple" , "strawberry" ]
shakes = fruits.copy()
print (shakes)
|
Output
['mango', 'apple', 'strawberry']
What is List Copy() Method?
The list Copy() function in Python is used to create a copy of a list. There are two main ways to create a copy of the list Shallow copy and Deep copy. We will discuss the list copy() method in detail below.
The list copy() function is used to create a copy of a list, which can be used to work and it will not affect the values in the original list. This gives freedom to manipulate data without worrying about data loss.
List copy() Method Syntax
list.copy()
Parameters
- The copy() method doesn’t take any parameters.
Returns: Returns a shallow copy of a list.
Note: A shallow copy means any modification in the new list won’t be reflected in the original list.
How to Create a Simple Copy of a List in Python
Copying and creating a new list can be done using copy() function in Python.
Python3
girls = [ "Priya" , "Neha" , "Radha" , "Nami" ]
girlstudent = girls.copy()
print (girlstudent)
|
Output
['Priya', 'Neha', 'Radha', 'Nami']
More Examples on List copy() Method
Let us see a few examples of the list copy() method.
Example 1: Simple List Copy
In this example, we are creating a List of Python strings and we are using copy() method to copy the list to another variable.
Python3
lis = [ 'Geeks' , 'for' , 'Geeks' ]
new_list = lis.copy()
print ( 'Copied List:' , new_list)
|
Output
Copied List: ['Geeks', 'for', 'Geeks']
Example 2: Demonstrating the working of List copy()
Here we will create a Python list and then create a shallow copy using the copy() function. Then we will append a value to the copied list to check if copying a list using copy() method affects the original list.
Python3
lis1 = [ 1 , 2 , 3 , 4 ]
lis2 = lis1.copy()
print ( "The new list created is : " + str (lis2))
lis2.append( 5 )
print ("The new list after adding new element : \
" + str (lis2))
print ("The old list after adding new element to new list : \
" + str (lis1))
|
Output
The new list created is : [1, 2, 3, 4]
The new list after adding new element : [1, 2, 3, 4, 5]
The old list after adding new element to new list : [1, 2, 3, 4]
Note: A shallow copy means if we modify any of the nested list elements, changes are reflected in both lists as they point to the same reference.
Shallow Copy and Deep Copy
A deep copy is a copy of a list, where we add an element in any of the lists, only that list is modified.
In list copy() method, changes made to the copied list are not reflected in the original list. The changes made to one list are not reflected on other lists except for in nested elements (like a list within a list).
We can use the copy.deepcopy() from the copy module to avoid this problem.
- Techniques to deep copy:
- Techniques to shallow copy:
- Using copy.copy()
- Using list.copy()
- Using slicing
To gain a deeper understanding, Refer to this article on Deep Copy vs Shallow Copy.
Demonstrating Techniques of Shallow and Deep copy
Here we will create a list and then create a shallow copy using the assignment operator, list copy() method, and copy.copy() method of the Python copy module.
We also create a deep copy using deepcopy() in Python. Then we will make changes to the original list and see if the other lists are affected or not.
Python3
import copy
list1 = [ 1 , [ 2 , 3 ] , 4 ]
print ( "list 1 before modification:\n" , list1)
list2 = list1
list3 = list1.copy()
list4 = copy.deepcopy(list1)
list1.append( 5 )
list1[ 1 ][ 1 ] = 999
print ( "list 1 after modification:\n" , list1)
print ( "list 2 after modification:\n" , list2)
print ( "list 3 after modification:\n" , list3)
print ( "list 4 after modification:\n" , list4)
|
Output
list 1 before modification:
[1, [2, 3], 4]
list 1 after modification:
[1, [2, 999], 4, 5]
list 2 after modification:
[1, [2, 999], 4, 5]
list 3 after modification:
[1, [2, 999], 4]
list 4 after mo...
Copy List Using Slicing
Here we are copying the list using the list slicing method [:] and we are appending the ‘a’ to the new_list. After printing we can see the newly appended character ‘a’ is not appended to the old list.
Python3
list = [ 2 , 4 , 6 ]
new_list = list [:]
new_list.append( 'a' )
print ( 'Old List:' , list )
print ( 'New List:' , new_list)
|
Output
Old List: [2, 4, 6]
New List: [2, 4, 6, 'a']
We discussed the definition, syntax and examples of list copy() method. copy() function is very useful when working with sensitive data and you can’t risk mistakes.
We also briefly talked about shallow vs deep copy. Hope this helped you in understanding copy() function in Python.
Read Other Python List Methods
More articles on to list copy:
Please Login to comment...