Using list and dict comprehension is simpler and computationally quicker than calling
Comprehensions are more efficient than calling
Given that these are completely equivalent:
Why not choose the easiest to both read, write, and quickest to execute (the first one)?
Do not follow this advice with set comprehension though, as when set comprehension is performed on an empty iterable it returns a
dict because Python cannot differentiate set comprehension and dict comprehension when working with an empty iterable:
four evaluated to an empty dict, which was unwanted behaviour.
If our GitHub code review bot spots this issue in your pull request it gives this advice: