. Advertisement .
..3..
. Advertisement .
..4..
Sometimes you may need a separate copy of a list, meaning any modification in a list doesn’t reflect on the other. You can achieve this by learning how to deep copy a list in Python with this guide.
Copy With The Assignment Statement
It is important to note that you can’t deep copy a list with a simple assignment statement. It is obviously the first solution most, if not all, have in mind when they need to create a copy of an object in Python. However, this doesn’t work in the way you might think.
This example demonstrates why the assignment statement is the wrong choice here:
>>> oldList = ['ITTutoria', 'Stack Overflow', 'Quora']
>>> newList = oldList
>>> oldList[2] = 'Reddit'
>>> oldList
['ITTutoria', 'Stack Overflow', 'Reddit']
>>> newList
['ITTutoria', 'Stack Overflow', 'Reddit']
In the example above, we assign an existing list with some elements to another list. However, when we modify the original list, you can see that the copied list changes as well.
This behavior can be traced to the fact that Python doesn’t create a whole new copy of an object when you assign it to a target located on the left side of the equal sign.
What it does is just create a reference to the original object and assign it to the right-sided object. There isn’t a new object. When it is modified, the new variable that refers to this object will observe those modifications as well.
Deep Copy A List In Python
With The copy Module
While its assignment statements don’t copy objects, Python comes with a standard module called copy for more advanced copying operations. If you need to create a separate copy of a list and modify it without changing the original list, this is what you are looking for.
The copy module has the deepcopy() function, which returns a deep copy of an object, such as a list. When invoked, it creates a new object and inserts copies found in the original object into it in a recursive manner.
>>> import copy
>>> oldList = ['ITTutoria', 'Stack Overflow', 'Quora']
>>> newList = copy.deepcopy(oldList)
>>> oldList[2] = 'Reddit'
>>> oldList
['ITTutoria', 'Stack Overflow', 'Reddit']
>>> newList
['ITTutoria', 'Stack Overflow', 'Quora']
Remember that you create a deep copy of other compound objects (such as class instances) in Python with this module as well. However, it doesn’t work with types like window, socket, file, stack frame, stack trace, method, module, and similar types.
By Slicing
Python allows you to slice a list, meaning to create a new list from a portion of the original list. Semicolons put in a pair of square brackets are the slicing notion:
[start:stop:step]
In this, start and stop are the indexes of the positions where you need the slicing to begin and end. Additionally, you can specify the step value too, which defaults to 1.
You can omit all of those parameters to slice the whole list. Since slicing creates a new list to store the elements of the sliced segment, this means you have a copy of that list too.
This is how you can slice a list and create a deep copy of it in Python:
>>> oldList = ['ITTutoria', 'Stack Overflow', 'Quora']
>>> newList = oldList[:]
>>> oldList[2] = 'Reddit'
>>> oldList
['ITTutoria', 'Stack Overflow', 'Reddit']
>>> newList
['ITTutoria', 'Stack Overflow', 'Quora']
With A List Comprehension
Programming languages like Python support list comprehensions, which are concise syntactic constructs you can use to create lists, such as splitting lists into chunks. Like the slicing operation, it creates a new list to store its elements.
This is how you can use a list comprehension to copy every element of the original list to a separate list:
>>> oldList = ['ITTutoria', 'Stack Overflow', 'Quora']
>>> newList = [i for i in oldList]
>>> oldList[2] = 'Reddit'
>>> oldList
['ITTutoria', 'Stack Overflow', 'Reddit']
>>> newList
['ITTutoria', 'Stack Overflow', 'Quora']
Summary
The assignment statement can’t help you deep copy a list in Python. But you can rely on the copy module, the slicing operation, or list comprehensions to get it done.
Leave a comment