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?
Curly braces. Passing keyword arguments into
dict(), though it works beautifully in a lot of scenarios, can only initialize a map if the keys are valid Python identifiers.This works:
This won’t work:
It will result in the following error: