Is a python dict comprehension always "last wins" if there are duplicate keys
<p>If I create a python dictionary with a dict comprehension, but there are duplicate keys, am I guaranteed that the last item will the the one that ends up in the final dictionary? It's not clear to me from looking at <a href="https://www.python.org/dev/peps/pep-0274/" rel="nofollow">https://www.python.org/dev/peps/pep-0274/</a>?</p>

<pre><code>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?
</code></pre>

<p>Last item wins. The best documentation I can find for this is in the <a href="https://docs.python.org/3/reference/expressions.html#dictionary-displays" rel="nofollow">Python 3 language reference, section 6.2.7</a>:</p>

<blockquote>
  <p>A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual âforâ and âifâ clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary <strong>in the order they are produced</strong>.</p>
</blockquote>

<p>That documentation also explicitly states that the last item wins for comma-separated key-value pairs (<code>{1: 1, 1: 2}</code>) and for dictionary unpacking (<code>{**{1: 1}, **{1: 2}}</code>):</p>

<blockquote>
  <p>If a comma-separated sequence of key/datum pairs is given, ... 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.</p>
  
  <p>A double asterisk <code>**</code> denotes <em>dictionary unpacking</em>. Its operand must be a mapping. Each mapping item is added to the new dictionary. Later values replace values already set by earlier key/datum pairs and earlier dictionary unpackings.</p>
</blockquote>

python dictionary duplicates dictionary-comprehension