Python List Copy: The Complete Guide

Copying a list in Python is quite simple. Python provides a copy() method that can help copy a list.

Python list copy

Python list copy() is a built-in function that returns a shallow copy of the list. The copy() method is used to copy all the items from one list to another. The List Copy() function does not change the original list is unchanged when the new list is modified.

Python3 introduced a new method in Python lists called copy(), and it does exactly what you think it does. It copies the list into another list.

One thing to remember is that it is not available in Python2. The copy() method takes around 1.488 seconds to complete.

Syntax

The syntax of the Python List Copy() method is the following.

newList = list.copy()

You have to provide the source list to copy the elements of that list.

Example

# app.py

targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']
clonedList = targaryens.copy()
print(clonedList)

See the output.

Python List Copy Example

The copy() function returns a list. However, it doesn’t modify the original list.

Copying and Modify the List

Let’s copy a list and then modify that list.

# app.py

targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']
clonedList = targaryens.copy()
print(clonedList)
print(targaryens)
targaryens.append('Daemon')
print('---After modified---')
print(targaryens)
print(clonedList)

In the above code, we first cloned the list, then modified the source list, which is targaryens. Then again, we have printed both lists.

See the output.

Copy and Modify List

You can see that an old list remains unchanged even when a new list is modified.

Shallow Copy of a List Using Slicing

If you do not know what a Python list slice is, check out Python Slice Notation.

Python slice() method is considered when we want to modify the list and also keep the copy of an original.

In this scenario, we copy the list and the reference. This process is also called cloning. The technique takes about 0.039 seconds and is the fastest technique.

# app.py

targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']
sliceCopy = targaryens[:]
print(sliceCopy)

See the output.

Shallow Copy of a List Using Slicing

Using the extend() method

The lists can be copied into a new list using the extend() function. The extend() method appends each element of the iterable object (e.g., another list) to the end of the new list. The extend() method takes around 0.053 seconds to complete.

# app.py

targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']

def Cloning(list): 
    copy = [] 
    copy.extend(list) 
    return copy 

extendedCopy = Cloning(targaryens) 
print("Original List:", targaryens) 
print("After Cloning:", extendedCopy)

See the output.

How To Clone Or Copy A List

Using list comprehension

The method of list comprehension can be used to copy all the items individually from one list to another. It takes around 0.217 seconds to complete.

# app.py

def Cloning(list):
  compreList = [i for i in list]
  return compreList

targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']
compreList = Cloning(targaryens)
print("Original List:", targaryens)
print("After Cloning:", compreList)

See the output.

Using list comprehension

Using the list() method

Python list() is the simplest method of cloning a list using the built-in function list(). It takes about 0.075 seconds to complete.

# app.py

def Cloning(l):
  cloneList = list(l) 
  return cloneList
  
targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']
cloneList = Cloning(targaryens)
print("Original List:", targaryens)
print("After Cloning:", cloneList)

See the output.

Using the list() method

Shallow and deep copy operations in Python

Assignment statements in Python do not copy objects; they create bindings between a target and an object.

A copy is sometimes needed for collections that are mutable or contain mutable elements so one can change a copy without changing the other.

The copy module provides generic shallow and deep copy operations. So we need to import that at the start of the program.

See the example of a shallow copy using the copy.copy() method.

# app.py

import copy

targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']
clonedList = copy.copy(targaryens)
print(clonedList)
print(targaryens)

targaryens.append('Daemon')
print('---After modified---')
print(targaryens)
print(clonedList)

See the output.

Shallow and deep copy operations in Python

The difference between deep and shallow copying is only relevant for compound objects. Compound objects are the objects that contain other objects, like lists or class instances.

  1. shallow copy constructs the new compound object and then, to the extent possible, injects references to the objects found in an original.
  2. deep copy constructs the new compound object and then, recursively, inserts copies into it of the objects found in the original.

Two problems often exist with deep copy operations that don’t exist with shallow copy operations:

  1. Recursive objects (compound objects that, directly or indirectly, contain the reference to themselves) may cause a recursive loop.
  2. Because deep copy copies everything, it may copy too much, such as the data intended to be shared between copies.

The deepcopy() function avoids these problems by:

  1. keeping the “memo” dictionary of objects already copied during a current copying pass; and
  2. letting user-defined classes override a copying operation or the set of the components copied.

Now, see the deepcopy() method example.

# app.py

import copy

targaryens = ['Aegon', 'Daenerys', 'Aemon', 'Aeris', 'Rhaegar', 'Viserys']
clonedList = copy.deepcopy(targaryens)
print('Original List:', targaryens)
print('Cloned List:', clonedList)
targaryens.append('Daemon')
print('---After modified---')
print('Original List:', targaryens)
print('Cloned List:', clonedList)

See the output.

How To Clone Or Copy A List in Python

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.