Standard for dict-contants with duplicate keys?

Tim Chase python.list at tim.thechases.com
Fri Sep 15 21:12:15 EDT 2017


On 2017-09-15 17:45, Terry Reedy wrote:
> On 9/15/2017 3:36 PM, Tim Chase wrote:
> >    d = {
> >       "a": 0,
> >       "a": 1,
> >       "a": 2,
> >        }
> > 
> > In my limited testing, it appears to always take the last one,
> > resulting in
> > 
> >    {"a": 2}
> > 
> > Is this guaranteed by the language spec
> 
> https://docs.python.org/3/reference/expressions.html#dictionary-displays
> If a comma-separated sequence of key/datum pairs is given, they are 
> evaluated from left to right to define the entries of the
> dictionary: each key object is used as a key into the dictionary to
> store the corresponding datum. This means that you can specify the
> same key multiple times in the key/datum list, and the final
> dictionary’s value for that key will be the last one given.

Ah, I'd checked the "Data Structures" and "Built-in types" pages, but
missed the "expressions" page.  At least that means that the botched
data in our system is at least *consistently* botched which eases my
work a bit.

Many thanks,

-tkc






More information about the Python-list mailing list