+ 3
Please tell me how its working🤔🤔
7 Antworten
+ 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
+ 4
Thanks everyone....🙂
+ 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
+ 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)
0
can anyone elaborate? i actually didn't get the loop concept