[docs] [issue31575] Functional Programming HOWTO sub-optimal example for reduce

Anran Yang report at bugs.python.org
Mon Sep 25 08:11:36 EDT 2017


New submission from Anran Yang:

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).

----------
assignee: docs at python
components: Documentation
messages: 302950
nosy: Anran Yang, docs at python
priority: normal
severity: normal
status: open
title: Functional Programming HOWTO sub-optimal example for reduce
versions: Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31575>
_______________________________________


More information about the docs mailing list