Re:Python List Comprehensions Explained Visually
by iamsohungry on Wednesday December 02, 2015 @10:30PM
(#10667561)
I think the problem here is that nested comprehensions and filter comprehensions are almost always too complex. If you are doing something that requires that, you should break it up and give the pieces names:
odd_numbers = range(1, limit, 2)
doubled_odd_numbers = [n * 2 for n in odd_numbers]
The one line list comprehension is as much readable for people familiar with Python as the variant with the explicit loop.
It is not uncommon to use several list comprehensions in a function. If you rewrite them as explicit loops then it may increase the line count 4 times e.g., it can convert a simple 3-lines function that could be understood at a glance into 12 lines function that you have to read line by line to understand -- it is easy to keep track of 3 values on 3 lines but it is not clear whether it is true for 12 lines.