Explanation:
๐น Line 1: Creating the List
n = [2, 4]
A list n is created with two elements: 2 and 4.
๐น Line 2: Creating the map Object
m = map(lambda x: x + 1, n)
map() applies the lambda function x + 1 to each element of n.
This does not execute immediately.
m is a map iterator (lazy object).
๐ Values inside m (not yet evaluated):
3, 5
๐น Line 3: First Use of map
print(list(m))
list(m) forces the map object to execute.
Elements are processed one by one:
2 + 1 = 3
4 + 1 = 5
The iterator m is now fully consumed.
Output:
[3, 5]
๐น Line 4: Second Use of map
print(sum(m))
The map iterator m is already exhausted.
No elements are left to sum.
sum() of an empty iterator is 0.
Output:
0
๐น Final Output
[3, 5]
0

0 Comments:
Post a Comment