In many programs we need to assign a list to another variable to create a duplicate list. But by just writing listA=listB we are not creating a duplicate list, it mean we are referring to the same list because the statement gives the memory location and values of the listB to listA, so both are same lists and eventually this leads to change in one list also makes the change in other list because both are same lists.
Example:
Here, when we applied remove operation on listB, the operation also got applied to listA which we did not intend to do.
So, some methods to create a list clone are:
1) listA=listB[:]
2) listA=listB.copy()
By using the above two methods we can overcome this list duplicity problem. We can also use for loop to append every element in listB to listA which is a long process.
Comments