+ 5

Can you please explain the output of this code?

a = [1,[2,3]] b = a[:] a[0] = 3 a[1][1] = 5 print(b) Answer [1,[2,5]]

5th Mar 2020, 1:09 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
10 Answers
+ 8
To understand this example, you need to understand and be able to imagine how different types of data (int,list,tuple etc.) are stored in memory. Every list in Python is stored in memory as a “box” with its’ own adress. This box contains different items (int, float, tuple, set, other lists etc.) You can easily change the items inside the box, while its’ adress remains the same. Actualy, what does it mean: a=[1,[2,3]] ?? It means that “a” is a box, in which the first item ihas reference to integer “1”, and second item has reference to another box, in which the first item has reference to int “2” and second item to int “3”. So in this example, when you see b=a[:] this actually means that we make a so called “shallow” copy of list “a”. We can have “shallow” and “deep” copies (as I know). “Shallow “ copy means that all “boxes” inside element “a” are not copied - so other boxes which are equal to those inside “a” are not created. If there is “deep” copy, so other boxes equal to inner boxes should be created.
5th Mar 2020, 7:16 PM
Mikhail Solovyev
Mikhail Solovyev - avatar
+ 7
a = [1,[2,3]] "a" contains reference to a list of two elements: [0] = "1" and [1] = reference(!) to the nested list with two elements [2,3] (you can think about it as [1, addressof[2,3]]) b = a[:] creates and writes to "b" copy of the "a" list, the 2nd element (with index 1) is copied as reference (address), new nested list is not created! "a" and "b" now are different lists, but 2nd elements of both "a" and "b" lists point to the same nested list [2, 3]. a[0] = 3 3 assigned to first element of "a" list a[1][1] = 5 a[1] contains reference(address) to [2,3] list so a[1][1] is the second element of [2,3] list which is equal to 3. that value is overwritten by 5 [2, 3] now become [2, 5] print(b) prints [1, [2, 5]] since second elements of "a" and "b" are equal references(addresses) to the same list [2, 5]
5th Mar 2020, 1:41 PM
andriy kan
andriy kan - avatar
+ 6
I didn't get it HonFu :(
5th Mar 2020, 1:27 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
5th Mar 2020, 6:48 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
+ 5
Ozair Khan I have just read that article, but still I didn't get list or not list the element of that list, why they don't change at the copy? They're both elements of the same list
5th Mar 2020, 7:08 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
+ 5
All JUMP_LINK__&&__Python__&&__JUMP_LINK Challenges are welcomed 🤖, the issue with the references comes up all the time here - it is tricky after all. I wanted to write a little something about it for quite a while, and now I did. I'd be glad to know if reading this will clarify the confusion. If you had understood already, then all the better, nevermind. 😉 https://code.sololearn.com/c89ejW97QsTN/?ref=app
5th Mar 2020, 10:34 PM
HonFu
HonFu - avatar
+ 5
Wow HonFu , I really appreciate your explanations 👏👏👏👏👏
6th Mar 2020, 6:21 AM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
+ 4
b = a[:] in words means: 'Make a new list with the content of the old one.' If you reassign an element of the copy now, it won't change in the original. The fourth line doesn't reassign something in the list b though, it reassigns an item of the inner list. That inner list is the same as the one in a. It is just an 'address card' to the very same object. With the second line, you have made a so-called 'shallow copy': You have a new list, but with (references to) the same content in it. If that content is mutable (like a list is), you still change it in both places.
5th Mar 2020, 1:22 PM
HonFu
HonFu - avatar
+ 3
So, in this example, when you see a[0] = 3 we change the reference of a[0], a[0] now has reference to integer “3”. But b[0] remains reference to integer “1”. When you see a[1][1] = 5 we change the reference inside the box. a[1] is a box, inside it there are two references to “2” and “3”. Here we change the second reference from integer “3” to integer “5”. But the second item in box “b” has referense to the same box as “a” has inside itself. That’s why when we change a[1][1], at the same time we change b[1][1], because it is the same box.
5th Mar 2020, 7:33 PM
Mikhail Solovyev
Mikhail Solovyev - avatar