I'm putting in some effort to learn Python, and I am paying close attention to common coding standards. This may seem like a pointlessly nit-picky question, but I am trying to focus on best-practices as I learn, so I don't have to unlearn any 'bad' habits later.
I see two common methods for initializing a dict:
a = {
'a': 'value',
'another': 'value',
}
b = dict(
a='value',
another='value',
)
Which is considered to be "more pythonic"? Which do you use? Why?
{}
pretty uniformly, but I see a lot of the explicitdict()
approach elsewhere. I could see the benefit of an explicit syntax, but the absence of the approach in the official docs made me suspicious. After posting this I looked at the library docs fordict
and found the caution that keys must be valid identifiers when an explicitdict
is used to initialize a dict. – daotoaddict()
is spelleddict
--it uses the name of the type. The braces ({}
) rely on punctuation to identify the type. – daotoad