Comprehension with two variables - explanation needed

Jon Ribbens jon+usenet at unequivocal.co.uk
Sun Nov 23 18:46:19 CET 2014


On 2014-11-23, Roy Smith <roy at panix.com> wrote:
> 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".

My reminder rule for nested comprehensions is "Which way do you
instinctively think they should nest and makes logical sense?
They don't work that way."



More information about the Python-list mailing list