Python List copy() Function
The List copy()
method creates a Shallow copy of the list.
Syntax
my_list.copy()
copy() Parameters
Python List copy()
function does not take any parameters.
copy() Return Value
Python List copy()
function returns a new list. It doesn't modify the original list.
Examples
Example 1: Copy a List with copy()
Consider this example in which:
original_list.copy()
creates a shallow copy oforiginal_list
.- When a string in the
copied_list
is modified, it does not affectoriginal_list
because the values in the lists are references to the same objects.
# create a list
original_list = ['Tom', 'Ryan', 'David']
# create a copy of the original_list
copied_list = original_list.copy()
print("Original List:", original_list)
print("Copied List:", copied_list)
# Modifying the copied list
copied_list[1] = 'Anna'
print("Original List after modification:", original_list)
print("Copied List after modification:", copied_list)
output
Original List: ['Tom', 'Ryan', 'David']
Copied List: ['Tom', 'Ryan', 'David']
Original List after modification: ['Tom', 'Ryan', 'David']
Copied List after modification: ['Tom', 'Anna', 'David']
Example 2: Shallow Copy of a List with Mutable Values
The copy()
method creates a Shallow copy!
In the following example the values in original_list
are lists (which are mutable) and so modifying one of the lists in copied_list
also affects the corresponding list in original_list
.
original_list = [['Tom', 'Ryan'], ['David']]
copied_list = original_list.copy()
print("Original List:", original_list)
print("Copied List:", copied_list)
# Modifying the copied dictionary
copied_list[1].append('Anna')
print("Original List after modification:", original_list)
print("Copied List after modification:", copied_list)
output
Original List: [['Tom', 'Ryan'], ['David']]
Copied List: [['Tom', 'Ryan'], ['David']]
Original List after modification: [['Tom', 'Ryan'], ['David', 'Anna']]
Copied List after modification: [['Tom', 'Ryan'], ['David', 'Anna']]
copy() vs Assignment statement
- When the
copy()
method is used, a new list is created which is filled with a copy of the references from the original list. - When the
=
operator is used, a new reference to the original list is created.
For example, an assignment statement does not copy objects.
old_list = ['Ryan', 'David', 'Tom']
new_list = old_list
new_list[0] = 'Anna'
print(old_list) # Output: ['Anna', 'David', 'Tom']
print(new_list) # Output: ['Anna', 'David', 'Tom']
output
['Anna', 'David', 'Tom']
['Anna', 'David', 'Tom']
When you execute new_list = old_list
, you don’t actually have two lists. The assignment just makes the two variables point to the one list in memory.
So, when you change new_list
, old_list
is also modified. If you want to change one copy without changing the other, use copy()
method.
old_list = ['Ryan', 'David', 'Tom']
new_list = old_list
new_list[0] = 'Anna'
print(old_list) # Output: ['Anna', 'David', 'Tom']
print(new_list) # Output: ['Anna', 'David', 'Tom']
output
{'name': 'Tom', 'age': 25}
{'name': 'xx', 'age': 25}
Equivalent Method using Slicing Operator
An alternative way to copy a list is assigning a slice of the entire list to a variable:
old_list = ['Anna', 'David', 'Tom']
new_list = old_list[:]
print(new_list) # Output: ['Anna', 'David', 'Tom']
output
['Anna', 'David', 'Tom']