This tutorial guides you on how to prevent the list that changes unexpected after assignment using copy or clone in Python programming language.
List that changes unexpected after assignment
I have created a list called list1 and assigned that list to a new list called list2 as shown below. Then any modifications to the new list list2 also changes the old list list1.
#create list >>> list1 = [1,2,3] #assignment >>> list2 = list1 >>> list1 [1, 2, 3] >>> list2 [1, 2, 3] #modifications to new list >>> list2.append(4) >>> list2 [1, 2, 3, 4] #impacted old list >>> list1 [1, 2, 3, 4]
Note, when you do list2 = list1, actually you don’t have two lists created, rather the assignment just copies the reference of the old list to the new list. Therefore, both lists list1 and list2 have the same reference so that you can treat them as same list after assignment.
Hence when you modify list2, list1 also changes.
Prevent the list unexpectedly changes after assignment
Now, lets see how to prevent the list unexpected changes after assignment using copy or clone.
It returns the shallow copy of the list. And it is equivalent to the following slice notation: “[:]”
>>> list1 = [1,2,3] #list.copy() >>> list2 = list1.copy() >>> list2 [1, 2, 3] >>> list2.append(4) >>> list2 list2 list2 [1, 2, 3, 4] >>> list1 [1, 2, 3]
Using slice notation
The slice notation is equivalent to list.copy() method.
>>> list1 = [1, 2, 3] #slice notation >>> list3 = list1[:] >>> list3 [1, 2, 3] >>> list3.append(4) >>> list3 [1, 2, 3, 4] >>> list1 [1, 2, 3]
Using list() function
Returns list whose elements are the same and in the same order as iterables in the list. Refer list function for more details.
>>> list1 = [1,2,3] #list() function >>> list2 = list(list1) >>> list2 [1, 2, 3] >>> list2.append(4) >>> list2 [1, 2, 3, 4] >>> list1 [1, 2, 3]
Using copy library
Basically, you can use shallow and deep copy operations using copy library and can change one copy without changing the other. This module provides two type of operations: shallow copy and deep copy.
Here is an example for shallow copy (copy.copy()).
>>> list1 = [1,2,3] >>> import copy ... list2 = copy.copy(list1) >>> list2 [1,2,3] >>> list2.append(4) >>> list2 [1,2,3,4] >>> list1 [1,2,3]
The following is an example for deep copy (copy.deepcopy())
>>> list1 = [1,2,3] >>> import copy ... list2 = copy.deepcopy(list1) >>> list2 [1,2,3] >>> list2.append(4) >>> list2 [1,2,3,4] >>> list1 [1,2,3]
That’s it. Basically, you had learnt how to prevent the list that changes unexpected after assignment using various approaches in Python programming language.
Hope it is helpful 🙂
You’ll also like:
- Convert list of lists to flat list – flatten list in Python ?
- Concatenate or join two lists using Python
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- Difference between Python’s list methods append and extend ?
- Python equivalent for && (logical and) to use in if statement
- TypeError: ‘list’ object is not callable
- How to check if the given list is empty in Python ?
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- Create list of lists in Python
- Extract numbers from a string in python
- How to access index and value in for loop – Python?
- Find index of an element in a list – Python