Explanation:
Line 1 — Create a list
nums = [1, 2, 3, 4]
A list named nums is created with four elements.
Current value of nums → [1, 2, 3, 4].
Line 2 — Create a filter object
f = filter(lambda x: x in nums, nums)
A filter object f is created.
The lambda checks: is x present in nums?
Important: filter is lazy — it does not run now, it runs only when iterated.
Line 3 — Modify the list
nums.pop()
Removes the last element (4) from nums.
Now nums becomes → [1, 2, 3].
Line 4 — Convert filter to list and print
print(list(f))
Now filter actually starts evaluating.
It loops over the original sequence (which is nums) but applies the condition using the current value of nums ([1, 2, 3]).
So:
1 in [1,2,3] → True
2 in [1,2,3] → True
3 in [1,2,3] → True
4 in [1,2,3] → False (because 4 was popped)
Final Output
[1, 2, 3]

0 Comments:
Post a Comment