0
0
Pythonprogramming~10 mins

Lambda with filter() in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lambda with filter()
Start with list
Apply filter()
Use lambda function
Check each item
Keep item
Return filtered list
End
The filter() function uses a lambda to check each item in a list, keeping only those where the lambda returns True.
Execution Sample
Python
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
This code filters the list to keep only even numbers using a lambda inside filter().
Execution Table
StepCurrent Item (x)Lambda Check (x % 2 == 0)ResultActionFiltered List So Far
111 % 2 == 0 → FalseFalseDiscard 1[]
222 % 2 == 0 → TrueTrueKeep 2[2]
333 % 2 == 0 → FalseFalseDiscard 3[2]
444 % 2 == 0 → TrueTrueKeep 4[2, 4]
555 % 2 == 0 → FalseFalseDiscard 5[2, 4]
💡 All items checked, filter ends with [2, 4]
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
xN/A12345N/A
filtered list[][][2][2][2, 4][2, 4][2, 4]
Key Moments - 2 Insights
Why does the filtered list only include 2 and 4?
Because the lambda returns True only for even numbers (2 and 4). See execution_table rows 2 and 4 where action is 'Keep'.
What happens when lambda returns False for an item?
The item is discarded and not added to the filtered list. See execution_table rows 1, 3, and 5 where action is 'Discard'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the filtered list after step 3?
A[1, 2, 3]
B[2, 3]
C[2]
D[]
💡 Hint
Check the 'Filtered List So Far' column at step 3 in execution_table.
At which step does the lambda function return True for the first time?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Lambda Check' and 'Result' columns in execution_table.
If the lambda was changed to 'x > 3', what would be the filtered list after step 5?
A[4, 5]
B[2, 4]
C[1, 2, 3]
D[]
💡 Hint
Consider which numbers in the original list are greater than 3.
Concept Snapshot
filter(function, iterable) returns items where function(item) is True
Use lambda for quick inline functions
Each item is tested; if True, item is kept
Result is an iterator, convert to list to see
Common for filtering lists simply
Full Transcript
This visual shows how filter() works with a lambda function in Python. We start with a list of numbers. The filter() applies the lambda to each number, checking if it is even (x % 2 == 0). For each item, if the lambda returns True, the item is kept; otherwise, it is discarded. The execution table shows each step: the current item, the lambda check, the result, the action taken, and the filtered list so far. Variables track the current item and the filtered list as it builds. Key moments clarify why only even numbers remain and what happens when lambda returns False. The quiz tests understanding of the filtered list at different steps and how changing the lambda affects results. The snapshot summarizes the syntax and behavior of filter() with lambda.