3 ms·
I don't see a conflict between loops, list comprehensions and other shortcuts - use the syntax sugar where it makes the code simpler/easier to read, otherwise
by scardine 9y ago
I don't see a conflict between loops, list comprehensions and other shortcuts - use the syntax sugar where it makes the code simpler/easier to read, otherwise stick to plain loops.
My own rules for "list/dict comprehension" versus "loops" versus "generator expressions" are the following:
* use a loop where you are interested only in the side-effects or where it will make the code easier to read
* use generator expressions where you don't really need to instantiate a list, for example when you are just iterating over the result and throwing it away.
* use list or dict comprehensions where you need to build and save a list/dict and the resulting code does not resemble a lisp program (where the choice does not make the program harder to read).
- weberc2 9y agoI agree, but it's nevertheless a violation of the "one obvious way" rule.