Please tell me how its working🤔🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
27th Feb 2020, 2:26 PM
ANJALI SAHU
5 Answers
+ 5
If you want to see how it's working, you can paste your code here: http://pythontutor.com/visualize.html#mode=edit It lets you visualize how the Python interpreter works line by line
27th Feb 2020, 2:34 PM
Alina Sansevich
Alina Sansevich - avatar
+ 4
Thanks everyone....🙂
28th Feb 2020, 4:00 AM
ANJALI SAHU
+ 2
Your for loop runs over the positions of your list: 0, 1, 2, 3 and so on, until it's gone. After you have erased the first element, position 1 (the next position) will be what was originally position 2, because the list is shrinking, while you're moving through it. [1, 2, 3, 4, 5] ^ 0 (erase one) [2, 3, 4, 5] ^ 1
27th Feb 2020, 2:30 PM
HonFu
HonFu - avatar
+ 2
If you start deleting from the end of the list it will work. After last iteration the list a is empty: a=[1,2,3,4,5] for i in reversed(a): # <<!! print(a) a.remove(i) print(a)
27th Feb 2020, 3:12 PM
Lothar
Lothar - avatar
0
can anyone elaborate? i actually didn't get the loop concept
12th Mar 2020, 7:41 AM
Tridib Das
Tridib Das - avatar