0

filter() definition simplified? (PY)

I just don't get it, or the syntax either.

21st Sep 2024, 4:15 AM
Xmosity
2 Réponses
+ 5
The Python filter() function is like a shortcut to writing a for loop. It is not a replacement for all for loops, but rather a specific kind with an if statement that chooses which items belong or don't belong in a group of items. You supply the items to filter in its second argument. Filter loops over those items and tests each one according to the conditional function that you supply in the first argument. Like a for loop, filter passes each item (one at a time) as an argument into the True/False function, then checks the return value. If your function returns false, then filter ignores that item and skips to the next item. If your function returns true, then filter includes that item in its output. filter(<True/False function>, <iterable items>)
21st Sep 2024, 5:25 AM
Brian
Brian - avatar
+ 1
Thanks
21st Sep 2024, 5:27 AM
Xmosity