0

After each iteration we are deleting one item from the list. But the value of x confuses me on how the for loop works internally.

arr=[0,1,2,3,4,5,6]
i=0
for x in arr:
 print(i,x,end=": ")
 print(arr)
 del arr[i]
 i=i+1

print(i,x,end=": ");print(arr)

Output

0 0: [0, 1, 2, 3, 4, 5, 6]
1 2: [1, 2, 3, 4, 5, 6]
2 4: [1, 3, 4, 5, 6]
3 6: [1, 3, 5, 6]
4 6: [1, 3, 5]
k_vishwanath
  • 1,326
  • 2
  • 20
  • 28

0 Answers0