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.
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.
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.
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.
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.
That’s it for this tutorial.