Tom Ritchford
Dec 26, 2023

--

This is a micro-optimization of no value.

In any real-world program, it's extremely unlikely that the dict constructor is going to appear anywhere in top fifty functions that are consuming your CPU time.

Premature optimization is bad. You should write the clearest, simplest and most correct code you can first, and then if it is too slow, profile it.

In this case, I'd write: `lengths = {w: len(w) for w in words}` because it's the clearest. (And yes, it's also likely to be the most performant too.)

--

--

No responses yet