0

I embrace to study Python thorugh the course material of pythoninstitute.org and one of the quizes consist of:

l1 = ["A", "B", "C"]
l2 = l1
l3 = l2
del l1[0]
del l2[:]
print(l3)

I expected ['B'. 'C'] as result of print(l3), meanwhile the correct result is [].

I don't understand why. Could you explain that to me?

To get what I expected that result I had to code as it follows:

l1 = ["A", "B", "C"]
l2 = l1
l3 = l2
del l1[0]
l4 = l2[:]
del l4
print(l3)

Could you explain the difference between the two snippet?

  • 1
    `l3 = l2` does _not_ copy `l2`, nor does `l2 = l1` copy `l1`. – ForceBru Oct 25 '20 at 12:53
  • Please see [here](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) and [here](https://stackoverflow.com/questions/17246693/what-is-the-difference-between-shallow-copy-deepcopy-and-normal-assignment-oper). – Mr. T Oct 25 '20 at 12:55
  • You will be able to understand this much more intuitively If you look up `pass by reference versus pass by value`. Even though here we are not using a function, the idea is the same. As a general rule, one can look at it this way - because the size of a collection is not known beforehand, a programming language will generally deal with the address of original collection rather than making a copy of it. You would have seen exact same behavior if you were assigning an array in java to three variables . – R.S. Oct 25 '20 at 13:15

4 Answers4

1

It is because, when you assign a list to another list, it just creates a reference through another variable for the same storage. l1,l2,l3 all refers to a same data.

when you remove a element from one list, the data gets reflected all the variable that reference that data.

in your second code, you could have got the same result with the below. basically you are removing value from 0 index from all the variables l1,l2,l3 by doing del l1[0] '

l1 = ["A", "B", "C"]
l2 = l1
l3 = l2
del l1[0]

print(l3)

'

Prem Anand
  • 49
  • 6
0
l2 = l1
l3 = l2

l2 points to the same memory location as l1, and consequently l3 points to the same as l2 and l1. Making any modifications in any of the three will reflect over all 3 variables.
When you do l4 = l1[:], l4 will become a copy of l1 and not just point to the same location as l1.

Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
0
del l[:]

This clear the entire list. And because all of this variables point to the same memory location , like it is mentioned in the previous answer, you get that result. Also take a look at the documentation. https://docs.python.org/3/tutorial/datastructures.html

ManosAnast
  • 11
  • 2
0

In python, use .copy() method to make a copy of a list, if you assign one list to another list it will share the same memory location. in the image you can find the difference between list.copy() and assign

lst1 = [1,2,3,4]
lst2 = lst1
lst3 = lst1.copy()
print("lst1 address : ", hex(id(lst1)))
print("lst2 address : ",hex(id(lst2)))
print("lst3 address : ",hex(id(lst3)))

lst1 address :  0x1f00c983f00
lst2 address :  0x1f00c983f00
lst3 address :  0x1f00a3127c0

If you delete elements in the list1, in list2 also it will delete because they share the same memory location.

Ravi Teja
  • 16
  • 3