Is understanding a python dict always a "last win" if there are duplicate keys

If I create a python dictionary with dict understanding, but there are duplicate keys, am I sure the last element will be the last in the final dictionary? I don’t understand looking at https://www.python.org/dev/peps/pep-0274/ ?

new_dict = {k:v for k,v in [(1,100),(2,200),(3,300),(1,111)]}
new_dict[1] #is this guaranteed to be 111, rather than 100?
+4
source share
2 answers

Last win. The best documentation I can find for this is in the Python 3 link, section 6.2.7 :

dict, , , , "" "". , .

, -, ({1: 1, 1: 2}) ({**{1: 1}, **{1: 2}}):

/datum,... key/datum, .

** . . . , key/datum .

+5

-

{key: val for (key, val) in pairs}

pairs - (, ) 2- , , , "".

, - , " ", . :

>>> n = 10
>>> pairs = [("a", i) for i in range(n)]
>>> {key:val for (key, val) in pairs}
{'a': 9}
>>> {key:val for (key, val) in set(pairs)}
{'a': 2}
+1

All Articles