Re:The Zen of Python by Example
by nmb on Thursday February 10, 2011 @03:47PM
(#2203643)
For the evens_only implementation, is there a reason to use map/filter vs a generator? eg:
seems to work just as well.
[ Parent
| on HN ]
With [...] you are doing the same as list(...), so your example would return a list:
>>> evens_only([1,2,3,4,5])
[1, 2]
If you want a generator instead: evens_only = lambda nums: (n/2 for n in nums if n%2==0)
So you'll get: >>> evens_only([1,2,3,4,5])
<generator object <genexpr> at 0x7f2ef79eaa00>
I don't know which one would be more Pythonic, clean and simple lambda one-liner or two-lined simple function.