+ 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]]
10 Antworten
+ 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.
+ 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]
+ 6
I didn't get it HonFu :(
+ 5
+ 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
+ 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
+ 5
Wow HonFu , I really appreciate your explanations đđđđđ
+ 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.
+ 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.