✅ Step-by-Step Explanation
🔹 1. This is your list:
nums = [0, 1, 2, 3, 4]It contains both falsy and truthy values.
🔹 2. This is the filter with lambda:
result = filter(lambda x: x, nums)lambda x: x means:
👉 Return the value itself.filter() keeps only values that are truthy.
-
In Python, these are falsy values:
0, None, False, ""
So 0 is removed, and all non-zero numbers remain.
🔹 3. Convert result to list:
print(list(result))Since filter() returns an iterator, we convert it to a list to display it.
✅ FINAL OUTPUT
[1, 2, 3, 4]Key Concept
👉 This line:
filter(lambda x: x, nums)means:
“Keep only the values that are True in a boolean sense.”


0 Comments:
Post a Comment