Python dict creation speed: Literal vs dict vs comprehension

Python dict creation speed: Literal vs dict vs comprehension

Want to create a Python dict? You have options; which is fastest?

– Literal, with {‘a’:10, ‘b’:20}: 37.8 ns
– Invoke dict(a=10, b=20): 58.5 ns
– Invoke dict on a list of tuples, dict([(‘a’, 10), (‘b’, 20)]): 103 ns
– Comprehension, {c : ord(c) for c in ‘ab’}: 78 ns