Is there a difference between using a dict literal and a dict constructor?
Using PyCharm, I noticed it offers to convert a dict literal:
d = {
'one': '1',
'two': '2',
}
into a dict constructor:
d = dict(one='1', two='2')
Do these different approaches differ in some significant way?
(While writing this question I noticed that using dict()
it seems impossible to specify a numeric key .. d = {1: 'one', 2: 'two'}
is possible, but, obviously, dict(1='one' ...)
is not. Anything else?)
Solution 1:
I think you have pointed out the most obvious difference. Apart from that,
the first doesn't need to lookup dict
which should make it a tiny bit faster
the second looks up dict
in locals()
and then globals()
and the finds the builtin, so you can switch the behaviour by defining a local called dict
for example although I can't think of anywhere this would be a good idea apart from maybe when debugging
Solution 2:
Literal is much faster, since it uses optimized BUILD_MAP and STORE_MAP opcodes rather than generic CALL_FUNCTION:
> python2.7 -m timeit "d = dict(a=1, b=2, c=3, d=4, e=5)"
1000000 loops, best of 3: 0.958 usec per loop
> python2.7 -m timeit "d = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}"
1000000 loops, best of 3: 0.479 usec per loop
> python3.2 -m timeit "d = dict(a=1, b=2, c=3, d=4, e=5)"
1000000 loops, best of 3: 0.975 usec per loop
> python3.2 -m timeit "d = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}"
1000000 loops, best of 3: 0.409 usec per loop
Solution 3:
They look pretty much the same on Python 3.2.
As gnibbler pointed out, the first doesn't need to lookup dict
, which should make it a tiny bit faster.
>>> def literal():
... d = {'one': 1, 'two': 2}
...
>>> def constructor():
... d = dict(one='1', two='2')
...
>>> import dis
>>> dis.dis(literal)
2 0 BUILD_MAP 2
3 LOAD_CONST 1 (1)
6 LOAD_CONST 2 ('one')
9 STORE_MAP
10 LOAD_CONST 3 (2)
13 LOAD_CONST 4 ('two')
16 STORE_MAP
17 STORE_FAST 0 (d)
20 LOAD_CONST 0 (None)
23 RETURN_VALUE
>>> dis.dis(constructor)
2 0 LOAD_GLOBAL 0 (dict)
3 LOAD_CONST 1 ('one')
6 LOAD_CONST 2 ('1')
9 LOAD_CONST 3 ('two')
12 LOAD_CONST 4 ('2')
15 CALL_FUNCTION 512
18 STORE_FAST 0 (d)
21 LOAD_CONST 0 (None)
24 RETURN_VALUE