How to ensure update() adds to end of dictionary
<p>I've read that <a href="http://stackoverflow.com/questions/20110627/print-original-input-order-of-dictionary-in-python">OrderedDict</a> can be used to keep information in a dictionary in order, but it seems to require that you input the information while instantiating the dictionary itself.</p>

<p>However, I need to be able to use update() multiple times in a for loop and ensure the each entry gets added to the <em>end</em> of the dictionary. Is there a way to do that?</p>

<p>Here's a visual of what I'm talking about</p>

<pre><code>for e in entryList:
    myDict.update({e[2]: e[3]})
</code></pre>

<p>This enters all the new info, but doesn't keep it in order.</p>

<p>Per the docs (<a href="https://docs.python.org/2/library/collections.html#collections.OrderedDict" rel="nofollow">https://docs.python.org/2/library/collections.html#collections.OrderedDict</a>) you'll have to delete the entry before adding it.</p>

<p>You can wrap this in a subclass for convenience..:</p>

<pre><code>class MyOrderedDict(OrderedDict):
    def update(self, other):
        for k, v in other.items():
            if k in self:
                 del self[k]
            self[k] = v
</code></pre>

<p>it seems strange to use update the way you do though:</p>

<pre><code>myDict.update({e[2]: e[3]})
</code></pre>

<p>since it would be more clear to write:</p>

<pre><code>myDict[e[2]] = e[3]
</code></pre>

<p>to make that work you'll also need to override <code>__setitem__</code></p>

<pre><code>class MyOrderedDict(OrderedDict):
    def __setitem__(self, key, val):
        if key in self:
             del self[key]
        super(self, MyOrderedDict).__setitem__(key, val)

    def update(self, other):
        for k, v in other.items():
            self[k] = v
</code></pre>

python dictionary