DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

How to copy a list in Python?

In this blog, we will learn about a new method in Python - copy list. While writing code, there are times when a user needs to reuse an object. Re-typing every single line of code seems useless. Hence, we use the copy method available in the list.

Table of contents

  1. Introduction to copy list
  2. Python copy list method
  3. Copy list using =
  4. Using built-in method list() to copy list
  5. Closing thoughts

Introduction to copy list

Easy as it sounds, copying a list in Python is quite simple. But we still have to follow the syntax and know when and how to use the copy list method. This method makes it easy to write parts of code that occur multiple times.

Just to make it clear, this method only works on the list.

Python copy list method

Python copy() method copies the list and returns the copied list. It does not take any parameter and returns a shallow copy of the list.

A shallow copy is the one that does not show any modification on the original list. The copied list points to a different memory location than the original list, so changing one list does not affect another list.

Syntax:

list_new = list.copy()
Enter fullscreen mode Exit fullscreen mode

Input:

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

#Copying list
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))
Enter fullscreen mode Exit fullscreen mode

Here, we created a list and then copied the list. When you add an element to the new list, you can see that the old list does not show the modification.

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]
Enter fullscreen mode Exit fullscreen mode

Copy list using equal operator

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

That said, if we make any modifications to the new list, the old list will also get modified.

Input:

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

#Copying list
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))
Enter fullscreen mode Exit fullscreen mode

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

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]
Enter fullscreen mode Exit fullscreen mode

Using built-in method list() to copy list

Another way to copy a list in Python is to use the built-in method list(). It also creates a shallow copy which means any time a modification is made in the new list, it will not show on the old list.

Input:

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

#Copying list
list_new = list(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)
Enter fullscreen mode Exit fullscreen mode

Here, we created a list and then copied the list. When you add an element to the new list, you can see that the old list does not show the modification.

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]
Enter fullscreen mode Exit fullscreen mode

Closing thoughts

Copy list is an easy way to copy in Python. It is a built-in method that makes writing code easier. You can use the other methods mentioned above but make sure you know about the shallow copy. One can read about more Python concepts here.

Top comments (0)