5 ms·
Python offers filtering expressions in its generator syntax. I find Python's "lambda" hurts readability for most uses, which pains me as a Lisp geek. Your exa
by swdunlop 14y ago
Python offers filtering expressions in its generator syntax. I find Python's "lambda" hurts readability for most uses, which pains me as a Lisp geek. Your example, as a generator:
halve_evens_only = (i / 2) for i in nums if (i%2 == 0)
The parens aren't necessary, but they help readability for people who aren't used to the generator order of operations. (Again, Lisp geek, more parens means more readable in my fractured mind.)
- dmg8 14y agoThe parens are required. What you've written raises a SyntaxError. You can omit them in the generator expression if it's being passed directly as the only parameter to a function: halve_evens_only = list(i/2 for i in nums if i % 2 != 0)