Re:Python List Comprehensions Explained Visually
by has2k1 on Wednesday December 02, 2015 @09:59PM
(#10667458)
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.
The parts in the comprehension
compares well to for normal looping behaviour without changing order of the iterations.One of my favourite comprehensions is of the sort
of course this can be the same as but the comprehension is more versatile For most once the mental fog clears comprehensions become natural very quickly as they are not that far removed from normal loops.