[Python-Dev] performance of {} versus dict()

Chris Angelico rosuav at gmail.com
Wed Nov 14 14:18:38 CET 2012

On Wed, Nov 14, 2012 at 8:12 PM, Chris Withers <chris at simplistix.co.uk> wrote:
> I suspect I'm not the only one who finds:
> a_dict = dict(
>     x = 1,
>     y = 2,
>     z = 3,
>     ...
>     )
> ...easier to read than:
> a_dict = {
>     'x':1,
>     'y':2,
>     'z':3,
>     ...
>     }
> What can we do to speed up the former case?

Perhaps an alternative question: What can be done to make the latter
less unpalatable? I personally prefer dict literal syntax to a dict
constructor call, but no doubt there are a number of people who feel
as you do. In what way(s) do you find the literal syntax less
readable, and can some simple (and backward-compatible) enhancements
help that?

I've seen criticisms (though I don't recall where) of Python,
comparing it to JavaScript/ECMAScript, that complain of the need to
quote the keys. IMO this is a worthwhile downside, as it allows you to
use variables as the keys, rather than requiring (effectively) literal
strings. But it does make a dict literal that much more "noisy" than
the constructor.


More information about the Python-Dev mailing list