How to copy a list in Python?
Tags: python, programming, beginners, tutorial
Published: 2022-03-31 06:11:21

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](#introduction-to-copy-list)
2.  [Python copy list method](#python-copy-list-method)
3.  [Copy list using =](#copy-list-using-equal-operator)
4.  [Using built-in method list() to copy list](#using-builtin-method-list-to-copy-list)
5.  [Closing thoughts](#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.  
<span id="section2"></span>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()

#### 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))

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]

<span id="section3"></span>  

## 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))

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]

<span id="section4"></span>  

## 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)

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]

<span id="section5"></span>  

## 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](https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy#:~:text=What%20is%20Shallow%20Copy%3F,the%20memory%20address%20is%20copied.). One can read about more Python concepts [here](https://flexiple.com/list-of-lists-python).