This tutorial teaches you about OrderedDict and how to use it in Python. It helps you arrange groups of items in a precise order. It keeps the order intact when you print them or access them.
Understand OrderedDict in Python
After seeing its name, you might have already inferred that it is an extended form of a Python dictionary. Now, let’s understand about it in more detail.
What is Python OrderedDict?
In Python, an OrderedDict is a special dictionary that remembers the order in which you add items. In the older versions of Python (before 3.7), the normal dictionary did not have this ability.
Think of an OrderedDict as a list of tasks you will do in a specific sequence. If you use a regular dictionary, the order might get mixed up. But with an OrderedDict, the sequence stays exactly how you put it.
How to Use OrderedDict in Python?
Using OrderedDict in Python helps you keep track of tasks exactly as you write them down. It’s like making a to-do list where you track what needs to be done first, second, and so on. Here’s how you can use it:
Step 1 First, you import OrderedDict from a special part of Python called collections. This lets your program remember the order of tasks.
Step 2 Imagine creating a shopping list in Python. You write down items like apples, bananas, milk, and bread. Each item has a specific amount you need to buy. When you look at your list, it shows every item and how much you should buy. Everything appears in the order you wrote it down.
from collections import OrderedDict
# Make an OrderedDict for a shopping list
items = {
'Apples': 5,
'Bananas': 3,
'Milk': 1,
'Bread': 2
}
shopping_list = OrderedDict(items)
# Print the shopping list
for item, quantity in shopping_list.items():
print(f"Buy {quantity} {item}")
Step 3 When you run your program, it displays these tasks in the same order you wrote them. This way, you can stay organized and know what comes next in your day.
Execution Time: 0.0018 seconds
Buy 5 Apples
Buy 3 Bananas
Buy 1 Milk
Buy 2 Bread
OrderedDict vs. Regular Dictionary
An OrderedDict in Python differs from a regular dictionary in one main way: it remembers the order in which you add items.
Regular Dictionary
A regular dictionary does not keep track of the order. When you add items, the order may not be the same when you retrieve them. Check this by running the following example.
# Creating a regular dictionary
regular_dict = {}
# Adding items
regular_dict["b"] = 2
regular_dict["a"] = 1
regular_dict["c"] = 3
# Printing the regular dictionary
for key, value in regular_dict.items():
print(f"{key}: {value}")
When you run, it gives this output:
b: 2
a: 1
c: 3
The items may come out in any order.
OrderedDict
An OrderedDict keeps items in the order you add them.
Example:
from collections import OrderedDict
# Creating an OrderedDict
ordered_dict = OrderedDict()
# Adding items
ordered_dict["b"] = 2
ordered_dict["a"] = 1
ordered_dict["c"] = 3
# Printing the OrderedDict
for key, value in ordered_dict.items():
print(f"{key}: {value}")
Output:
b: 2
a: 1
c: 3
The items come out in the exact order you added them.
Before You Leave
We hope you are now clear about the use and advantages of the OrderedDict dictionary in Python. However, you should note that even a regular dictionary starts preserving the order from Python 3.7 onwards. So, it is viable to use only if you are running with an old version of Python than 3.7.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Facebook) if you gained some knowledge from this tutorial.
Enjoy learning,
TechBeamers.