10th Feb 2019, 5:03 AM
Dan Rhamba
Dan Rhamba - avatar
4 Respostas
+ 2
When the list x is created it had eight items [0, 1, 2, 3, 4, 5, 6, 7] The first time through the for loop i is the first item in the list or 0. The 0 is removed from the list which becomes [1, 2, 3, 4, 5, 6, 7] The second time the for loop sets i to the second item in the list which is now 2 not the original 1. The 2 is removed and the list is now [1, 3, 4, 5, 6, 7] The third time though the for loop i becomes the third item in the list 4. The 4 is removed and the list is now [1, 3, 5, 6, 7] The fourth time through the for loop i becomes the fourth item in the list 6. The 6 is removed and the list becomes [1, 3, 5, 7] There is no fifth item in the list now so the for loop ends.
10th Feb 2019, 5:38 AM
Joseph Whalen
+ 2
x.remove(i) removes the item i from the list not the item at index i. If you have a list x = [1,2,3,4,5,6,7] x.remove(1) will remove the 1 at index 0 from the list, not the 2 from x[1].
10th Feb 2019, 6:59 AM
Joseph Whalen
+ 2
To understand better print(i) and print(x) inside the loop you will get to know each and every step in the process.
19th Feb 2019, 6:38 AM
Vanga Balaji Ravi Teja
Vanga Balaji Ravi Teja - avatar
+ 1
You're right Joseph Whalen ... SORRY!
10th Feb 2019, 8:34 AM
unChabon
unChabon - avatar