Explanation:
๐น 1. List Creation
nums = [None, 0, False, 1, 2]
A list named nums is created.
It contains different types of values:
None → represents no value
0 → integer zero
False → boolean false
1, 2 → positive integers
๐ In Python:
None, 0, False are falsy values
1, 2 are truthy values
๐น 2. Using filter() Function
res = list(filter(bool, nums))
filter(function, iterable) applies a function to each item.
Only elements where the function returns True are kept.
๐ Here:
bool is used as the function.
Each element is checked like this:
bool(value)
๐น 3. Filtering Process (Step-by-Step)
Element bool(value) Result
None False ❌ Removed
0 False ❌ Removed
False False ❌ Removed
1 True ✅ Kept
2 True ✅ Kept
๐ After filtering:
[1, 2]
๐น 4. Converting to List
filter() returns a filter object (iterator).
list() converts it into a proper list.
๐น 5. Printing Output
print(res)
Displays the final filtered list.
✅ Final Output
[1, 2]

0 Comments:
Post a Comment