What is happening?
🔹 1. map() creates an iterator
res = map(lambda x: x + 1, nums)This does NOT create a list immediately. It creates a lazy iterator:
res → (2, 3, 4)…but nothing is calculated yet.
🔹 2. The for loop consumes the iterator
-
This loop runs through all values: 2, 3, 4
-
But since we used pass, nothing is printed
-
Important: After this loop, the iterator is now EXHAUSTED
🔹 3. Printing after exhaustion
print(list(res))res is already empty
-
So converting it to a list gives:
✅ Final Output
[]Key Tricky Rule
A map() object can be used ONLY ONCE.
Once it is looped through, it becomes empty forever.
✅ Correct Way (If you want reuse)


0 Comments:
Post a Comment