Comprehension with two variables - explanation needed

Roy Smith roy at panix.com
Sun Nov 23 10:57:09 EST 2014


In article <mailman.16191.1416757555.18130.python-list at python.org>,
 Skip Montanaro <skip.montanaro at gmail.com> wrote:

> > But it breaks all the picture that I've built in my head about comps till
> > now...
> 
> Note that list comprehensions are little more than syntactic sugar for for
> loops. If you're having terrible writing or understanding one, especially a
> compound one like your example, it can help to write it as a (nested) for
> loop, then covert it (perhaps incrementally) to the list comp form.

Or not.  If it was complicated enough that you needed to loopify it to 
understand what it's doing, have pity on the next person who has to 
maintain your code and leave it as a loop :-)

My general rule for code hygiene is, "If I have to think about whether 
something is too complicated, it is".



More information about the Python-list mailing list