a = [1,2,3] for a[-1] in a: print(a[-1]) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

a = [1,2,3] for a[-1] in a: print(a[-1])

Try to solve this simple question why is the output 1,2,2 why 1,2,3

18th Oct 2020, 11:41 AM
Abdisa merga
Abdisa merga - avatar
1 Answer
+ 3
a[-1] assigned 1 on first iteration,list a is now [1,2,1] a[-1] assigned 2 on second iteration ,list a is now [1,2,2] a[-1] is assigned 2 again during third iteration , And so you can see why it prints 1,2,2
18th Oct 2020, 11:50 AM
Abhay
Abhay - avatar