How is length 3? What is the working and process i.e how is code getting executed on a line by line basis? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How is length 3? What is the working and process i.e how is code getting executed on a line by line basis?

a=[1,2] d=a a.append(7) print(len(d)) The output i.e length of d is 3, even though I have appended 7 to a, not d. How? What is the process of execution on a line by line basis.

16th Sep 2020, 9:32 AM
Shashank Hegde
Shashank Hegde - avatar
1 Answer
+ 9
d=a Means you passed a as reference not as copy. So changes made in a also affect d . If you don't want it use d=a.copy()
16th Sep 2020, 9:34 AM
TOLUENE
TOLUENE - avatar