[Answered ]-How can I write my own OrderedDict class in Python?

2👍

The class would manage a dictionary and a list. Behind the scenes, for every standard dict operation it will use both:

Dvalues = {"foo":"x","bar":"y","baz":"z"}
Lorder = ["bar","baz","foo"]

To get a value by key, as a standard dictionary:

return Dvalues[key]

To get a value by the order m it was entered:

return Dvalues[Lorder[m]]

Add key,value:

Dvalues[key] = value
Lorder.append(key)

etc.

0👍

You might take a look at the source in Python 2. It seems that it is implemented in C for Python 3.

Leave a comment