Explanation:
Initial List Creation
a = [0, 1, 2]
A list a is created with three elements.
Length of a = 3
Loop Setup
for i in range(len(a)):
len(a) is evaluated once at the start → 3
range(3) generates values: 0, 1, 2
Loop will run 3 times, even though a changes later
First Iteration (i = 0)
a = list(map(lambda x: x + a[i], a))
a[i] → a[0] → 0
map() adds 0 to every element of a
Calculation:
[0+0, 1+0, 2+0] → [0, 1, 2]
Updated list:
a = [0, 1, 2]
Second Iteration (i = 1)
a[i] → a[1] → 1 (from updated list)
Calculation:
[0+1, 1+1, 2+1] → [1, 2, 3]
Updated list:
a = [1, 2, 3]
Third Iteration (i = 2)
a[i] → a[2] → 3 (value has changed)
Calculation:
[1+3, 2+3, 3+3] → [4, 5, 6]
Updated list:
a = [4, 5, 6]
Final Output
print(a)
Output
[4, 5, 6]


0 Comments:
Post a Comment