How do you retrieve items from a dictionary in the order that they're inserted?

PythonDictionary

Python Problem Overview


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

Python Solutions


Solution 1 - Python

The standard Python dict does this by default if you're using CPython 3.6+ (or Python 3.7+ for any other implementation of Python).

On older versions of Python you can use collections.OrderedDict.

Solution 2 - Python

Use OrderedDict(), available since version 2.7

Just a matter of curiosity:

from collections import OrderedDict
a = {}
b = OrderedDict()
c = OrderedDict()

a['key1'] = 'value1'
a['key2'] = 'value2'

b['key1'] = 'value1'
b['key2'] = 'value2'

c['key2'] = 'value2'
c['key1'] = 'value1'

print a == b  # True
print a == c  # True
print b == c  # False

Solution 3 - Python

As of Python 3.7, the standard dict preserves insertion order. From the docs:

>Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was implementation detail of CPython from 3.6.

So, you should be able to iterate over the dictionary normally or use popitem().

Solution 4 - Python

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.)

class ordered_dict(dict):
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        self._order = self.keys()

    def __setitem__(self, key, value):
        dict.__setitem__(self, key, value)
        if key in self._order:
            self._order.remove(key)
        self._order.append(key)

    def __delitem__(self, key):
        dict.__delitem__(self, key)
        self._order.remove(key)

    def order(self):
        return self._order[:]

    def ordered_items(self):
        return [(key,self[key]) for key in self._order]


od = ordered_dict()
od["hello"] = "world"
od["goodbye"] = "cruel world"
print od.order()            # prints ['hello', 'goodbye']

del od["hello"]
od["monty"] = "python"
print od.order()            # prints ['goodbye', 'monty']

od["hello"] = "kitty"
print od.order()            # prints ['goodbye', 'monty', 'hello']

print od.ordered_items()
# prints [('goodbye','cruel world'), ('monty','python'), ('hello','kitty')]

Solution 5 - Python

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.

Solution 6 - Python

Or, just make the key a tuple with time.now() as the first field in the tuple.

Then you can retrieve the keys with dictname.keys(), sort, and voila!

Gerry

Solution 7 - Python

I've used StableDict before with good success.

http://pypi.python.org/pypi/StableDict/0.2

Solution 8 - Python

Or use any of the implementations for the PEP-372 described here, like the odict module from the pythonutils.

I successfully used the pocoo.org implementation, it is as easy as replacing your

my_dict={}
my_dict["foo"]="bar"

with

my_dict=odict.odict()
my_dict["foo"]="bar"

and require just this file

Solution 9 - Python

It's not possible unless you store the keys in a separate list for referencing later.

Solution 10 - Python

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?

Solution 11 - Python

What you can do is insert the values with a key representing the order inputted, and then call sorted() on the items.

>>> obj = {}
>>> obj[1] = 'Bob'
>>> obj[2] = 'Sally'
>>> obj[3] = 'Joe'
>>> for k, v in sorted(obj.items()):
...     print v
... 
Bob
Sally
Joe
>>> 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionreadonlyView Question on Stackoverflow
Solution 1 - PythondF.View Answer on Stackoverflow
Solution 2 - PythonrewgoesView Answer on Stackoverflow
Solution 3 - PythonBrian McCutchonView Answer on Stackoverflow
Solution 4 - PythonEli CourtwrightView Answer on Stackoverflow
Solution 5 - PythonSerafina BrociousView Answer on Stackoverflow
Solution 6 - PythonGerryView Answer on Stackoverflow
Solution 7 - PythonjaywgravesView Answer on Stackoverflow
Solution 8 - PythonDavideView Answer on Stackoverflow
Solution 9 - PythonJeremy CantrellView Answer on Stackoverflow
Solution 10 - PythonRonald HobbsView Answer on Stackoverflow
Solution 11 - PythonA.J. UppalView Answer on Stackoverflow