3 ms·
When reading code I find comprehensions easier to comprehend than combinations of map, filter and reduce. That is considering that I played around with some Com
by has2k1 11y ago
When reading code I find comprehensions easier to comprehend than combinations of map, filter and reduce. That is considering that I played around with some Common-Lisp before getting into Python.
The parts in the comprehension
[*wanted* *iteration(s?)* *condition?*]
compares well to
*iterations(s?)*
*condition?*
*append wanted*
for normal looping behaviour without changing order of the iterations.
One of my favourite comprehensions is of the sort
[item for item in items in for i in (0, 1)]
of course this can be the same as
list(items) * 2
but the comprehension is more versatile
[item for item in items in for i in (0, 1) if items[0] != 'a']
For most once the mental fog clears comprehensions become natural very quickly as they are not that far removed from normal loops.
- deleted 11y ago[deleted]