Q. Predict the output of following two parts. Are the outputs same? Are the outputs different? Why?
(a)
L1, L2 = [2, 4], [2, 4]
L3 = L2
L2[1] = 5
print(L3)
(b)
L1, L2 = [2, 4], [2, 4]
L3 = list(L2)
L2[1] = 5
print (L3)
Answer =
Output:
(a)
[2, 5]
(b)
[2, 4]
Because (a) is true copy which mean L1 & L2 are like aliases for the same list. So for if we change list then L1 & L2 value also change.
While in (b) L3 is true independent copy of a list L2. So for after executing program if we change list of L2 then list of L3 will not change.
Pls share the coding
ReplyDeleteCopy the code of questions.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )