Do not overdo comprehensions and generators
We have seen how powerful comprehensions and generator expressions can be. However, we find that the more you try to do within a single comprehension or generator expression, the harder it becomes to read, understand, and therefore maintain or change.
If you consider the Zen of Python again, there are a few lines that, we think, are worth keeping in mind when dealing with optimized code:
>>> import this
...
Explicit is better than implicit.
Simple is better than complex.
...
Readability counts.
...
If the implementation is hard to explain, it's a bad idea.
...
Comprehensions and generator expressions are more implicit than explicit, can be quite difficult to read and understand, and can be difficult to explain. Sometimes, you have to break them apart using the inside-out technique to understand what is going on.
To give you an example, let us talk a bit more about Pythagorean triples. Just to remind you, a...