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>If you mean something like </p>

<pre><code>{key: val for (key, val) in pairs}
</code></pre>

<p>where <code>pairs</code> is an ordered collection (eg, list or tuple) of 2-element lists or tuples then yes, the comprehension will take the collection in order and the last value will "win". </p>

<p>Note that if pairs is a set of pairs, then there is no "last item", so the outcome is not predictable. 
EXAMPLE:</p>

<pre><code>&gt;&gt;&gt; n = 10
&gt;&gt;&gt; pairs = [("a", i) for i in range(n)]
&gt;&gt;&gt; {key:val for (key, val) in pairs}
{'a': 9}
&gt;&gt;&gt; {key:val for (key, val) in set(pairs)}
{'a': 2}
</code></pre>

python dictionary duplicates dictionary-comprehension