News for hackers. Stuff that matters.
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:

   evens_only = lambda nums: [n/2 for n in nums if n%2==0]
seems to work just as well.
Re:The Zen of Python by Example by drdaeman on Thursday February 10, 2011 @06:49PM (#2204441)
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.
Re:The Zen of Python by Example by l0nwlf on Thursday February 10, 2011 @04:27PM (#2203827)
I think that is List Comprehension and not Generator. 'Return' would be replaced by 'Yield' in case of a generator.