CARVIEW |
- Log in to:
- Community
- DigitalOcean
- Sign up for:
- Community
- DigitalOcean

Python OrderedDict is a dict subclass that maintains the items insertion order. When we iterate over an OrderedDict, items are returned in the order they were inserted. A regular dictionary doesn’t track the insertion order. So when iterating over it, items are returned in an arbitrary order. When we want to make sure that items are returned in the order they were inserted, we can use OrderedDict.
Python OrderedDict
- OrderedDict is part of python collections module.
- We can create an empty
OrderedDict
and add items to it. If we create an OrderedDict by passing a dict argument, then the ordering may be lost because dict doesn’t maintain the insertion order. - If an item is overwritten in the OrderedDict, it’s position is maintained.
- If an item is deleted and added again, then it moves to the last.
- OrderedDict
popitem
removes the items in FIFO order. It accepts a boolean argumentlast
, if it’s set toTrue
then items are returned in LIFO order. - We can move an item to the beginning or end of the OrderedDict using
move_to_end
function. It accepts a boolean argumentlast
, if it’s set toFalse
then item is moved to the start of the ordered dict. - From python 3.6 onwards, order is retained for keyword arguments passed to the OrderedDict constructor, refer PEP-468.
- We can use
reversed()
function with OrderedDict to iterate elements in the reverse order. - Equality tests between OrderedDict objects are order-sensitive and are implemented as
list(od1.items())==list(od2.items())
. - Equality tests between OrderedDict and other Mapping objects are order-insensitive like regular dictionaries. This allows OrderedDict objects to be substituted anywhere a regular dictionary is used.
Python OrderedDict Examples
Let’s look at some code examples of Python OrderedDict.
Creating OrderedDict object
from collections import OrderedDict
# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}
# creating empty ordered dict
ordered_dict = OrderedDict()
print(ordered_dict)
# creating ordered dict from dict
ordered_dict = OrderedDict(my_dict)
print(ordered_dict)
Output:
OrderedDict()
OrderedDict([('kiwi', 4), ('apple', 5), ('cat', 3)])
Adding, Replacing, Removing items from OrderedDict
# adding elements to dict
ordered_dict['dog'] = 3
# replacing a dict key value
ordered_dict['kiwi'] = 10
print(ordered_dict)
# removing and adding a value
ordered_dict.pop('kiwi')
print(ordered_dict)
ordered_dict['kiwi'] = 4
print(ordered_dict)
Output:
OrderedDict([('kiwi', 10), ('apple', 5), ('cat', 3), ('dog', 3)])
OrderedDict([('apple', 5), ('cat', 3), ('dog', 3)])
OrderedDict([('apple', 5), ('cat', 3), ('dog', 3), ('kiwi', 4)])
OrderedDict move_to_end example
# moving apple to end and dog to start
ordered_dict.move_to_end('apple')
ordered_dict.move_to_end('dog', False)
print(ordered_dict)
Output:
OrderedDict([('dog', 3), ('cat', 3), ('kiwi', 4), ('apple', 5)])
OrderedDict popitem example
# pop last item
item = ordered_dict.popitem(True)
print(item)
print(ordered_dict)
Output:
('apple', 5)
OrderedDict([('dog', 3), ('cat', 3), ('kiwi', 4)])
OrderedDict Reverse Iteration
# reversed iteration
for item in reversed(ordered_dict):
print(item)
Output:
kiwi
cat
dog
OrderedDict Equality Tests Example
# equality tests
d1 = {'a': 'A', 'b': 'B'}
d2 = {'b': 'B', 'a': 'A'}
od1 = OrderedDict({'a': 'A', 'b': 'B'})
od2 = OrderedDict({'b': 'B', 'a': 'A'})
print(d1 == d2)
print(od1 == od2)
print(d1 == od1)
Output:
True
False
True
You can download the complete example code from our GitHub Repository.
Reference: Python Docs
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
About the author
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Still looking for an answer?
- Table of contents
- Python OrderedDict
- Python OrderedDict Examples
Deploy on DigitalOcean
Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.
Become a contributor for community
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
DigitalOcean Documentation
Full documentation for every DigitalOcean product.
Resources for startups and SMBs
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Get our newsletter
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
The developer cloud
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Get started for free
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.