+ 6
why this output?
m=[0,1,2,3,4,5,6,7] for i in range(3): m.pop(i) print(m) somebody pls. explain this... https://code.sololearn.com/cjb22jU94U8V/?ref=app
4 Respuestas
+ 10
Running through the different iterations of the loop:
In the first iteration, the value of i is set to 0. This value is going to be the index that will be removed from the list (from the line m.pop(i)), with this iteration removing the first element from the array (0). This leaves it being [1,2,3,4,5,6,7]
In the next iteration, the value of i is set to 1. Considering that the first element just got removed, the element at index 1 is now 2, and so will be removed. Now, the array is [1,3,4,5,6,7]
Third iteration, i is 2, which removed the element at index 2 within the array (removing the 4 from it), leaving it to be [1,3,5,6,7]
Because the range function will iterate through every number below the value specified, the for loop stops there, resulting in the array left from the final iteration
+ 5
Thanks to all for clearing my doubt😊
+ 2
for i in range(3): #0,1,2
m.pop(i) #pop takes in index
👇
m.pop(0) #[0,1,2,3,4,5,6,7]
[1,2,3,4,5,6,7]
👇
m.pop(1) #[1,2,3,4,5,6,7]
[1,3,4,5,6,7]
👇
m.pop(2) #[1,3,4,5,6,7]
[1,3,5,6,7]