on a statement followed by an expression

Alan Bawden alan at csail.mit.edu
Sat Jun 4 17:32:48 EDT 2022


Meredith Montgomery <mmontgomery at levado.to> writes:

   How can I execute a statement followed by a value in a single line?

   def roberry_via_reduce(rs):
     return my_reduce(rs, lambda d, r: ``increment and return d'', {})

The grammar or Python is deliberately designed so that the body of a
lambda expression cannot contain any statements.  So your original code,
where you defined a separate `count_in' procedure, is probably what you
want.

Although using `reduce' is kind of silly in this case because you aren't
computing a new dictionary every time, but instead you are mutating the
_same_ dictionary every time.  Your original code that used a `for' loop
is actually much clearer.

-- 
Alan Bawden


More information about the Python-list mailing list