0

Does the assignment operator work as a deepcopy() or copy()?

For example, if I assign a list a to list b, does it create a shallow copy or a deep copy of list a?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

0

It is neither copy() nor deepcopy().

In Python, all variables are of reference type. So once you assign one list to another then they both represent the same address. So if you update/add/delete element of the list then the same changes will happen in list b also.

So if the list is having only immutable elements then you can use copy() to create a copy but if the list is having mutable elements as well so you need to use deepcopy()

import copy
b=a.copy()    #If list a elements are immutable
c=a.deepcopy()   #If list a elements are mutable
Avishka Dambawinna
  • 1,180
  • 1
  • 13
  • 29