Python OrderedDict – Reordering, Delete and Reinsert New OrderedDict
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Weβve been talking about classes from the Python collections module- Python namedtuple, and Python defaultdict. Today, we base our article on Python OrderedDict, another class in βcollectionsβ.
Here we study what Python OrderedDict is with its syntax and examples. Moreover, we will study comparing a regular dictionary and reordering an OrderedDict in Python.
At last, we will cover how to delete and reinsert a key for a new ordereddict, and the popitem method.
So, let’s start the Python OrderedDict Tutorial.
What is Python OrderedDict?
Python OrderedDict is just like a regular dictionary in Python, but it also saves the order in which pairs were added. Actually, it is a subclass of βdictβ.
Features of using OrderedDict in Python:
- Order preservation: It maintains the same sequence since the data was added.
- No effect: The values can be changed without affecting the original order.
- Equality checks consider order: Both order and content are being checked for equality, but different orders are not equal.
>>> issubclass(OrderedDict,dict)
Output
True
OrderedDict Syntax in Python
To define an OrderedDict in Python, we import it from the module βcollectionsβ. Let us see this Python ordereddict example.
>>> from collections import OrderedDict
Then, we call this factory function and assign it to a variable in Python.
>>> d=OrderedDict()
We now add some key-value pairs to it.
>>> d['a']=1 >>> d['e']=5 >>> d['d']=4 >>> d['b']=2 >>> d['c']=3
Now, if we access βdβ, we see that it preserved the order in which we defined the key-value pairs.
>>> d
Output
OrderedDict([(‘a’, 1), (‘e’, 5), (‘d’, 4), (‘b’, 2), (‘c’, 3)])
Comparing to a Regular Dictionary
Letβs try this with a regular dictionary. If we declared this as a regular dictionary, it would produce the pairs according to how the keys are stored in a hash table.
This depends on a random value to reduce collisions.
1. Equality
To a regular dictionary, the order does not matter. Let u take another Python ordereddict example.
>>> {'a': 1, 'e': 5, 'd': 4, 'b': 2, 'c': 3}=={'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}Output
True
To Python, these two are equal. Letβs create another OrderedDict, and check if it is the same as the OrderedDict βdβ we defined above.
>>> e=OrderedDict() >>> e['a']=1 >>> e['b']=2 >>> e['c']=3 >>> e['d']=4 >>> e['e']=5 >>> e
Output
OrderedDict([(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 4), (‘e’, 5)])
Now, letβs check if theyβre equal.
>>> d==e
Output
False
As you can see, theyβre not.
How to Reorder Python OrderedDict?
To reorder a key-value pair to the front or the end of an OrderedDict in Python, we have the move_to_end() method. Look, this is βdβ:
>>> d
Output
OrderedDict([(‘a’, 1), (‘e’, 5), (‘d’, 4), (‘b’, 2), (‘c’, 3)])
Now, letβs move (βeβ,5) to the end.
>>> d.move_to_end('e')
>>> dOutput
OrderedDict([(‘a’, 1), (‘d’, 4), (‘b’, 2), (‘c’, 3), (‘e’, 5)])
This method takes a key as an argument. But it may also take another argument- βlastβ. This may take one of two values: True or False.
When true, the item is moved to the end. And when false, it is moved to the front.
>>> d.move_to_end('a',last=True)
>>> dOutput
OrderedDict([(‘d’, 4), (‘b’, 2), (‘c’, 3), (‘e’, 5), (‘a’, 1)])
Here, we moved (βaβ,1) to the end. Now, letβs move it back to the front.
>>> d.move_to_end('a',last=False)
>>> dOutput
OrderedDict([(‘a’, 1), (‘d’, 4), (‘b’, 2), (‘c’, 3), (‘e’, 5)])
Now, to sort this, we do the following:
>>> d.move_to_end('b')
>>> dOutput
OrderedDict([(‘a’, 1), (‘d’, 4), (‘c’, 3), (‘e’, 5), (‘b’, 2)])
>>> d.move_to_end('c')
>>> dOutput
OrderedDict([(‘a’, 1), (‘d’, 4), (‘e’, 5), (‘b’, 2), (‘c’, 3)])
>>> d.move_to_end('d')
>>> dOutput
OrderedDict([(‘a’, 1), (‘e’, 5), (‘b’, 2), (‘c’, 3), (‘d’, 4)])
>>> d.move_to_end('e')
>>> dOutput
OrderedDict([(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 4), (‘e’, 5)])
Assigning a Key Again in Python OrderedDict
Letβs define a new OrderedDict in Python for this example.
>>> f=OrderedDict() >>> f['c']=3 >>> f['e']=5 >>> f['a']=1 >>> f['b']=2 >>> f['d']=4 >>> f
Output
OrderedDict([(‘c’, 3), (‘e’, 5), (‘a’, 1), (‘b’, 2), (‘d’, 4)])
Now, letβs try changing the value of key βeβ.
>>> f['e']=7 >>> f
Output
OrderedDict([(‘c’, 3), (‘e’, 7), (‘a’, 1), (‘b’, 2), (‘d’, 4)])
Okay. Since the key βeβ already exists, the old order is preserved; it isnβt ordered to the end.
Deleting and Reinserting a Key
When we delete a key and then reinsert it, it is ordered to the end of the new order. Letβs try deleting βeβ.
>>> f.pop('e')Output
7
>>> f
Output
OrderedDict([(‘c’, 3), (‘a’, 1), (‘b’, 2), (‘d’, 4)])
Now, letβs insert it again.
>>> f['e']=7 >>> f
Output
OrderedDict([(‘c’, 3), (‘a’, 1), (‘b’, 2), (‘d’, 4), (‘e’, 7)])
As you can see, it is at the back of the list now.
Popitem() in Python
The popitem() method in Python may take 0 or 1 argument- βlastβ. When βlastβ is true, LIFO is followed to delete; otherwise, FIFO is followed. When we donβt pass an argument, the last is assumed to be true.
Letβs take a new Python OrderedDict for this.
>>> g=OrderedDict() >>> g['d']=4 >>> g['b']=2 >>> g['a']=1 >>> g['c']=3 >>> g['e']=5 >>> g
Output
OrderedDict([(‘d’, 4), (‘b’, 2), (‘a’, 1), (‘c’, 3), (‘e’, 5)])
Now, letβs start popping.
>>> g.popitem(last=False)
Output
(‘d’, 4)
>>> g.popitem()
Output
(‘e’, 5)
>>> g.popitem(last=True)
Output
(‘c’, 3)
So, this was all about Python OrderedDict Tutorial. Hope you like our explanation.
Python Interview Questions on OrderedDict
1. What is an OrderedDict in Python?
2. Why use Python OrderedDict?
3. How is Ordereddict implemented in Python?
4. How to update Ordereddict in Python?
5. Give an example of Python OrderedDict.
Conclusion
Hopefully, weβve covered every important detail about Python orderedDict in this article.
We saw how to define them, Syntax, deleting and reinserting, assigning A key to Python ordereddict, and we saw methods move_to_end(), popitem(), and pop().
OrderedDict kept insertion order before Python 3.7 made that standard, but still offers methods like move_to_end for cache work.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google


I think you are missing a line of code. In section 7, the page reads:
Now, letβs insert it again.
>>> f
should there be a line above this that says something along the lines of “>>> f[‘e’] = 7” ?
Thanks for highlighting the issue, it has been rectified.