Explanation:
1️⃣ Creating the List
nums = [1,2,3]
Explanation
A list named nums is created.
It contains three elements.
nums → [1, 2, 3]
2️⃣ Creating the map Object
m = map(lambda x: x+10, nums)
Explanation
map() applies a function to each element of the list.
The function here is:
lambda x: x + 10
So the transformation would be:
1 → 11
2 → 12
3 → 13
⚠ Important:
map() does NOT calculate immediately.
It creates an iterator that produces values one by one when needed.
So internally:
m → iterator producing [11, 12, 13]
3️⃣ First next() Call
print(next(m))
Explanation
next() retrieves the next value from the iterator.
First element:
1 + 10 = 11
Output:
11
Now iterator position moves forward.
Remaining values:
[12, 13]
4️⃣ Second next() Call
print(next(m))
Explanation
Second element is processed:
2 + 10 = 12
Output:
12
Remaining values in iterator:
[13]
5️⃣ Modifying the Original List
nums.append(4)
Explanation
Now the list becomes:
nums → [1,2,3,4]
⚠ Important concept:
map() reads from the original list dynamically.
So the iterator will also see the new element 4.
Remaining iterator values now become:
3 + 10 = 13
4 + 10 = 14
Remaining:
[13, 14]
6️⃣ Calculating Sum of Remaining Iterator
print(sum(m))
Explanation
Remaining values are:
13 + 14
= 27
Output:
27
✅ Final Output
11
12
27

0 Comments:
Post a Comment