+ 6

Can you please explain the output of this code?

A = [1, 2, 3, 4, 1] for n in A: A[n] = 0 print(A) Answer is [0, 0, 3, 0, 1]

13th Mar 2020, 9:37 AM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
1 Answer
+ 6
for in loops through an iterator of list A. When pointing to first element: n is 1 A[1] = 0 so A becomes [1, 0, 3, 4, 1] When pointing to second element: n is 0 A[0] = 0 so A becomes [0, 0, 3, 4, 1] ...
13th Mar 2020, 9:43 AM
Gordon
Gordon - avatar