Python List copy()

The copy() method returns a shallow copy of the list.

A list can be copied with = operator. For example:

old_list = [1, 2, 3]
​new_list = old_list

The problem with copying the list in this way is that if you modify the new_list, the old_list is also modified.

old_list = [1, 2, 3]
new_list = old_list

# add element to list
new_list.append('a')

print('New List:', new_list )
print('Old List:', old_list )

When you run the program, the output will be:

Old List: [1, 2, 3, 'a']
New List: [1, 2, 3, 'a']

However, if you need the original list unchanged when the new list is modified, you can use copy() method. This is called shallow copy.


The syntax of copy() method is:

new_list = list.copy()

copy() parameter

The copy() method doesn't take any parameters.

Return Value from copy()

The copy() function returns a list. It doesn't modify the original list.


Example 1: Copying a List

# mixed list
list = ['cat', 0, 6.7]

# copying a list
new_list = list.copy()

# Adding element to the new list
new_list.append('dog')

# Printing new and old list
print('Old List: ', list)
print('New List: ', new_list)

When you run the program, the output will be:

Old List: ['cat', 0, 6.7]
New List: ['cat', 0, 6.7, 'dog']

You can see that, the old list remains unchanged even when the new list is modified.

You can also achieve the same result using slicing as follows:


Example 2: Shallow Copy of a List Using Slicing

# mixed list
list = ['cat', 0, 6.7]

# copying a list using slicing
new_list = list[:]

# Adding element to the new list
new_list.append('dog')

# Printing new and old list
print('Old List: ', list)
print('New List: ', new_list)