An example:
y = [1, 0, 0, 1]
y = list(map(lambda i: [0, 1] if i == 1 else [1, 0], y))
print(y)
This runs correctly without error:
[[1, 0], [0, 1], [0, 1], [1, 0]]
However, if I assign the map object to a variable and THEN convert it to a list like this:
y = [1, 0, 0, 1]
y = map(lambda i: [0, 1] if i == 1 else [1, 0], y)
y = list(y)
print(y)
It causes an error:
Traceback (most recent call last):
File "...\test.py", line 3, in <module>
y = list(y)
File "...\test.py", line 2, in <lambda>
y = map(lambda i: [0, 1] if i == 1 else [1, 0], y)
TypeError: 'map' object is not subscriptable
What is the mechanism behind this? I am confused even though I do have some basic understandings of Python bytecodes and assembly.
Using Python 3.9.7.
Thank you all form stackoverflow! I did not expect such quick help before this post, which is my very first.
I think @Barmar 's detailed answer solves the problem, and @ThierryLathuille 's comment on the question briefly pointed out the core underlying mechanism for me. My sincere gratitude goes for both!