28th Jan 2018, 10:52 PM
Nicolas Poulain
Nicolas Poulain - avatar
5 Réponses
+ 2
Your answer is line 5. In the second round, a and b are names for the same list. In the first round, 5 breaks that link by assigning a different list to b.
28th Jan 2018, 10:57 PM
John Wells
John Wells - avatar
+ 2
Thank you for that very clear answer !
28th Jan 2018, 11:10 PM
Nicolas Poulain
Nicolas Poulain - avatar
+ 2
Three bright lights on the spot. I can see clearly now. Thank you all for sharing knowledge !
29th Jan 2018, 6:52 PM
Nicolas Poulain
Nicolas Poulain - avatar
+ 1
# round 1 listA = [0] listB = listA # listB assigned to listA listB = listB + [1] # listB points to listB, then add element valued at 1 listB.append(2) # listB is appended element valued 2 print(listB) print(listA) # round 2 listA = [0] listB = listA # listB once again pointing to listA. listB.append(2) # listB still pointing at listA during append so listA is appended 2 listB = listB + [1] # listB now pointing to listB (not listA) when we append 1 print(listB) print(listA) Read my comments carefully. Basically when listB = listB it is now a copy. It does _not_ point to listA any more. So in Round 1 you appended 2 to the copy and in Round 2 you appended 2 to listA (it was still pointing to). Hope this makes sense.
28th Jan 2018, 11:20 PM
non
+ 1
Here I would like to refer listA as a And listB as b for easier typing a=[0] means a points to 0 b=a means b also points to 0 Pictorially it looks like this: a=>[0]<=b note::: => represent a pointing to After assignment both variables a & b point to the same element 0 append is a list method.It alters the original list. b.append(2) means 2 is added to the end of [0].Hence in picture a=>[0,2]<=b b=b+[1] means you are creating a copy of the original list & adding 1 to it Picture this as: a=>[0,2] b=>[0,2,1] try to print both lists listA & listB in your round2 to understand this program By the way, I appreciate you for such a wonderful question...Happy coding
29th Jan 2018, 3:51 AM
Sri Lakshmi
Sri Lakshmi - avatar