Last Updated: September 09, 2019
·
918
· wyuenho

Prefer {} over dict() in Python 2.7

Doug Hellman has recently done some investigations into the performance impact of using dict() instead of {} in Python 2.7 and he's concluded that dict() is 6 times more costly than {} because it does a lot more work under the hood to convert keyword arguments to a dict.

http://www.doughellmann.com/articles/misc/dict-performance/

2 Responses
Add your response

It's fair to point out it only applies to CPython 2.7. My friend tells me that there's virtually no difference on 3.3 or PyPy.

over 1 year ago ·

@milancermak Yep, but regardless, {} is still preferred over dict() due to succinctness.

over 1 year ago ·