<p>Is it possible to retrieve items from a Python dictionary in the order that they were inserted?</p>

[{'answer_id': 60852, 'author': 'Serafina Brocious', 'author_id': 4977, 'author_profile': 'https://Stackoverflow.com/users/4977', 'pm_score': 3, 'selected': False, 'text': "<p>You can't do this with the base dict class -- it's ordered by hash.  You could build your own dictionary that is really a list of key,value pairs or somesuch, which would be ordered.</p>\n"}, {'answer_id': 60862, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 7, 'selected': True, 'text': '<p>The standard Python <code>dict</code> does this by default if you\'re using CPython 3.6+ (or Python 3.7+ for any other implementation of Python).</p>\n\n<p>On older versions of Python you can use <a href="https://docs.python.org/library/collections.html#ordereddict-objects" rel="noreferrer"><code>collections.OrderedDict</code></a>.</p>\n'}, {'answer_id': 61031, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 4, 'selected': False, 'text': '<p>The other answers are correct; it\'s not possible, but you could write this yourself.  However, in case you\'re unsure how to actually implement something like this, here\'s a complete and working implementation that subclasses dict which I\'ve just written and tested. (Note that the order of values passed to the constructor is undefined but will come before values passed later, and you could always just not allow ordered dicts to be initialized with values.)</p>\n\n<pre><code>class ordered_dict(dict):\n    def __init__(self, *args, **kwargs):\n        dict.__init__(self, *args, **kwargs)\n        self._order = self.keys()\n\n    def __setitem__(self, key, value):\n        dict.__setitem__(self, key, value)\n        if key in self._order:\n            self._order.remove(key)\n        self._order.append(key)\n\n    def __delitem__(self, key):\n        dict.__delitem__(self, key)\n        self._order.remove(key)\n\n    def order(self):\n        return self._order[:]\n\n    def ordered_items(self):\n        return [(key,self[key]) for key in self._order]\n\n\nod = ordered_dict()\nod["hello"] = "world"\nod["goodbye"] = "cruel world"\nprint od.order()            # prints [\'hello\', \'goodbye\']\n\ndel od["hello"]\nod["monty"] = "python"\nprint od.order()            # prints [\'goodbye\', \'monty\']\n\nod["hello"] = "kitty"\nprint od.order()            # prints [\'goodbye\', \'monty\', \'hello\']\n\nprint od.ordered_items()\n# prints [(\'goodbye\',\'cruel world\'), (\'monty\',\'python\'), (\'hello\',\'kitty\')]\n</code></pre>\n'}, {'answer_id': 64266, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': "<p>if you don't need the dict functionality, and only need to return tuples in the order you've inserted them, wouldn't a queue work better?</p>\n"}, {'answer_id': 65326, 'author': 'jaywgraves', 'author_id': 9212, 'author_profile': 'https://Stackoverflow.com/users/9212', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve used StableDict before with good success.</p>\n\n<p><a href="http://pypi.python.org/pypi/StableDict/0.2" rel="nofollow noreferrer">http://pypi.python.org/pypi/StableDict/0.2</a></p>\n'}, {'answer_id': 65991, 'author': 'Gerry', 'author_id': 372664, 'author_profile': 'https://Stackoverflow.com/users/372664', 'pm_score': 3, 'selected': False, 'text': '<p>Or, just make the key a tuple with time.now() as the first field in the tuple.</p>\n\n<p>Then you can retrieve the keys with dictname.keys(), sort, and voila!</p>\n\n<p>Gerry</p>\n'}, {'answer_id': 103211, 'author': 'Jeremy Cantrell', 'author_id': 18866, 'author_profile': 'https://Stackoverflow.com/users/18866', 'pm_score': 0, 'selected': False, 'text': "<p>It's not possible unless you store the keys in a separate list for referencing later.</p>\n"}, {'answer_id': 659817, 'author': 'Davide', 'author_id': 25891, 'author_profile': 'https://Stackoverflow.com/users/25891', 'pm_score': 1, 'selected': False, 'text': '<p>Or use any of the implementations for the <a href="http://www.python.org/dev/peps/pep-0372/" rel="nofollow noreferrer">PEP-372</a> described <a href="http://www.python.org/dev/peps/pep-0372/#reference-implementation" rel="nofollow noreferrer">here</a>, like the <a href="http://www.voidspace.org.uk/python/odict.html" rel="nofollow noreferrer">odict module</a> from the <a href="http://www.voidspace.org.uk/python/pythonutils.html" rel="nofollow noreferrer">pythonutils</a>.</p>\n\n<p>I successfully used the pocoo.org implementation, it is as easy as replacing your</p>\n\n<pre><code>my_dict={}\nmy_dict["foo"]="bar"\n</code></pre>\n\n<p>with</p>\n\n<pre><code>my_dict=odict.odict()\nmy_dict["foo"]="bar"\n</code></pre>\n\n<p>and require just <a href="http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py" rel="nofollow noreferrer">this file</a></p>\n'}, {'answer_id': 24028185, 'author': 'A.J. Uppal', 'author_id': 3113477, 'author_profile': 'https://Stackoverflow.com/users/3113477', 'pm_score': -1, 'selected': False, 'text': "<p>What you can do is insert the values with a key representing the order inputted, and then call <code>sorted()</code> on the items.</p>\n\n<pre><code>&gt;&gt;&gt; obj = {}\n&gt;&gt;&gt; obj[1] = 'Bob'\n&gt;&gt;&gt; obj[2] = 'Sally'\n&gt;&gt;&gt; obj[3] = 'Joe'\n&gt;&gt;&gt; for k, v in sorted(obj.items()):\n...     print v\n... \nBob\nSally\nJoe\n&gt;&gt;&gt; \n</code></pre>\n"}, {'answer_id': 26637218, 'author': 'rewgoes', 'author_id': 2848021, 'author_profile': 'https://Stackoverflow.com/users/2848021', 'pm_score': 4, 'selected': False, 'text': "<p>Use OrderedDict(), available since version 2.7</p>\n<p>Just a matter of curiosity: <br /></p>\n<pre><code>from collections import OrderedDict\na = {}\nb = OrderedDict()\nc = OrderedDict()\n\na['key1'] = 'value1'\na['key2'] = 'value2'\n\nb['key1'] = 'value1'\nb['key2'] = 'value2'\n\nc['key2'] = 'value2'\nc['key1'] = 'value1'\n\nprint a == b  # True\nprint a == c  # True\nprint b == c  # False\n</code></pre>\n"}, {'answer_id': 51777540, 'author': 'Brian McCutchon', 'author_id': 2093695, 'author_profile': 'https://Stackoverflow.com/users/2093695', 'pm_score': 4, 'selected': False, 'text': '<p>As of Python 3.7, the standard dict preserves insertion order. From the <a href="https://docs.python.org/3.7/library/stdtypes.html#mapping-types-dict" rel="noreferrer">docs</a>:</p>\n\n<blockquote>\n  <p>Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was implementation detail of CPython from 3.6.</p>\n</blockquote>\n\n<p>So, you should be able to iterate over the dictionary normally or use <code>popitem()</code>.</p>\n'}]
['https://Stackoverflow.com/questions/60848', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']