What is the output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the output?

n = 5 ar1 = ar2 = [] for i in range(n): ar1.append(0) ar2.append (0) print(len(ar1))

16th May 2020, 3:41 AM
Sagar Singh
Sagar Singh - avatar
1 Answer
+ 2
The unexpected result is 10 because ar1 and ar2 are two lists linked to the same object. So at every loop both lists are expanded by 2 elements. If you want to define 2 differents lists you must write ar1,ar2 = [],[] instead of ar1 = ar2 = []
16th May 2020, 4:35 AM
Bilbo Baggins
Bilbo Baggins - avatar