There is this code:
a = [1, 2, 3, 4, 5]
a[:] = [] # and now a is also empty
Statement a[:] creates a copy of list a as I read, so if empty list [] is assigned to the copy then why original object is also modified?
x = a[:] makes x a new list containing the same values as a
a[:] = x makes the existing list a contain the same values as x
The behaviour of an expression changes when it switches sides of the equals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With