I initiate a list as below -
arr = [1,2,3,4,5]
and assign two lists to it as below -
arrA = arr
arrB = arr
When I do arrA.pop(0), why does it also pops the 0th element from arrB and arr? What is the logic behind this lists behavior in Python ?
I initiate a list as below -
arr = [1,2,3,4,5]
and assign two lists to it as below -
arrA = arr
arrB = arr
When I do arrA.pop(0), why does it also pops the 0th element from arrB and arr? What is the logic behind this lists behavior in Python ?
because you just copy the address of the arr value (like pointers in C).
to independently copy the value to another list, you can use
arrA = arr.copy()
or
arrA = arr[:]
when you pop the arr, the value in arrA doesn't change.