Modules in Python: OrderedDict

Let's discover OrderedDict and its functions.

We'll cover the following...

About OrderedDict

Python’s collections module has another great subclass of dict known as OrderedDict. As the name implies, this dictionary keeps track of the order of the keys as they are added. If we create a regular dict, we will note that it is an unordered data collection:

Press + to interact
from collections import Counter
Counter("superfluous")
counter = Counter("superfluous")
d = {"banana": 3, "apple": 4, "pear": 1, "orange": 2}
print(d)

Every time we print it out, the order may be different. There are times when we will need to loop over the keys of our dictionary in a specific order. For example, we have a use case where we needed the keys sorted so we could loop over ...

Access this course and 1400+ top-rated courses and projects.