Python Copy List Method (With Examples)

In this article, we will discuss the Python Copy List method, and we will learn this method with the help of examples.

What is Python List copy()?

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

It copies the list and returns the copied list.

Python Copy List

Example Program:

# mixed list
prime_numbers = [2, 3, 5]


numbers = prime_numbers.copy()


print('Copied List:', numbers)

Output:

Copied List: [2, 3, 5]

copy() List Python Syntax

The syntax of copy() list in Python is:

list_new = list.copy()

copy() Parameters

The copy() method does not take any parameter.

copy() Return Value

The copy() method returns a new list. It doesn’t modify the original list.

Copying A List in Python

Here’s the example program:

#Defining a list
list = ['a','b','c']


list_new = list.copy()

print ("This is the new list: " + str(list_new))

list_new.append('d')

print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))

Here, we created a list and then copied it. When you try to add an element to the new list, you can see that the old list doesn’t show the modification.

Also read the other python tutorial: Python Pi with Advanced Example

Program Output:

This is the new list: ['a', 'b', 'c']
The new list after adding a new element: ['a', 'b', 'c', 'd']
The old list after adding a new element['a', 'b', 'c']

Copy List Using =

We can also use the = operator to copy a list. The only difficulty of this method is that it does not create a shallow copy.

#Defining a list
list = ['a','b','c']



list_new = list


print ("This is the new list: " + str(list_new))


list_new.append('d')


print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))

Here, we created a list and then copied it. When you try to add an element to the new list, you can see the old list is modified, as this method does not create a shallow copy.

Program Output:

This is the new list: ['a', 'b', 'c']
The new list after adding a new element: ['a', 'b', 'c', 'd']
The old list after adding a new element['a', 'b', 'c', 'd']

Copy List Using Slicing Syntax

This is the other way to copy a list using slicing.

Example Program:

#Defining a list
list = ['a','b','c']


#Copying list using slicing
list_new = list[:]


print ("This is the new list: " + str(list_new))


list_new.append('d')


print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))

Here, we created a list and then copied it. When you try to add an element to the new list, you can see the old list is modified as this method creates shallow copy using the slicing syntax.

Program Output:

This is the new list: ['a', 'b', 'c']
The new list after adding a new element: ['a', 'b', 'c', 'd']
The old list after adding a new element['a', 'b', 'c']

Conclusion

The Copy List method is an easy way to copy. This is a built-in method that can make writing code easier. Also, you can use the other method discussed above, but make sure you know about shallow copy. I hope this simple Python tutorial can help you copy a list.

Inquiries

If you have any questions or suggestions about this tutorial, Copy List, please feel free to comment below.

Thank You!

Leave a Comment