Message302950
At the end of the Functional Programming HOWTO document (https://docs.python.org/3.7/howto/functional.html) the usage of reduce/lambda/for loops are compared and discussed. However, the example for reduce seems sub-optimal and thus the discussion is not that efficient. The example:
total = functools.reduce(lambda a, b: (0, a[1] + b[1]), items)[1]
could be changed to:
total = functools.reduce(lambda total, item: total + item[1], items, 0)
which is much more readable and is actually not much inferior to the loop one (though the sum approach is still more concise). |
|
Date |
User |
Action |
Args |
2017-09-25 12:11:36 | Anran Yang | set | recipients:
+ Anran Yang, docs@python |
2017-09-25 12:11:35 | Anran Yang | set | messageid: <1506341496.0.0.82723851335.issue31575@psf.upfronthosting.co.za> |
2017-09-25 12:11:35 | Anran Yang | link | issue31575 messages |
2017-09-25 12:11:35 | Anran Yang | create | |
|