3 ms·
> Better to use code that doesn't raise any questions, even if it's a few more characters. Certainly, I agree; I would usually use: (x for x in xs if x) Or
by dragonwriter 4y ago
> Better to use code that doesn't raise any questions, even if it's a few more characters.
Certainly, I agree; I would usually use:
(x for x in xs if x)
Or, if I know more about the kind of falsy values xs actually needs removed, something more explicit like:
(x for x in xs if x is not None)
Because Python’s multiplicity of falsy values can also be something of a footgun (particularly, when dealing with something a collection of Optionals where the substantive type has a falsy value like 0 or [] included.)
Instead of:
filter(None, xs)
Which is terse but potentially opaque.
Though it's additional syntax, I kind of wish genexp/list/set comprehensions could use something like “x from” as shorthand for “x for x in”, which would be particularly nice for filtering comprehensions.