Step-by-Step Explanation
1️⃣ Create the list
clcoding = [1, 2, 3, 4]2️⃣ Apply filter
f = filter(lambda x: x % 2 == 0, clcoding)✅ This creates a filter object (iterator) that will return only even numbers
✅ Important: filter() does NOT run immediately — it waits until you convert it to a list or loop over it.
3️⃣ Modify the original list
clcoding.append(6)Now the list becomes:
[1, 2, 3, 4, 6]4️⃣ Convert filter to list
print(list(f))Now the filter actually runs, using the updated list.
✅ Even numbers from [1, 2, 3, 4, 6] are:
[2, 4, 6]✅ Final Output
[2, 4, 6]⭐ Key Concept (Trick)
✅ filter() is lazy — it evaluates only when needed,
✅ So it always uses the latest version of the list.


0 Comments:
Post a Comment