OrderedDict in PythonThe OrderedDict is the subclass of the dict object in Python. The difference between dict and OrderedDict is that the OrderedDict itself maintains the orders of the keys as inserted, whereas in the dict, the order of the keys is not an important part. The OrderedDict is the standard library class. It is located in the collections module. For using it, the user has to import the collections standard library module. Example: In this article, we will discuss some of the operations on OrderedDict and how Dict is different from OrderedDict. The user can put some keys and a lot of them with values in the Dict class and OrderedDict class. In the following example, we will show how the ordering of the Dict class can vary, but for the OrderedDict class, it will remain fixed. Example: Output: Dict: ('PP', 10) ('QQ', 20) ('RR', 30) ('SS', 40) ('TT', 50) ('UU', 60) OrderedDict: ('PP', 10) ('QQ', 20) ('RR', 30) ('SS', 40) ('TT', 50) ('UU', 60) Changing the Value of the Specific KeyThe order of the keys will not be changed for the OrderedDict class after changing the value of the specific key, but in Dict class, the ordering may or may not change. Example: Output: Dict: ('PP', 10) ('QQ', 20) ('RR', 30) ('SS', 40) ('TT', 50) ('UU', 60) After changing value of specific key in Dict ('PP', 10) ('QQ', 111) ('RR', 30) ('SS', 40) ('TT', 50) ('UU', 60) OrderedDict: ('PP', 10) ('QQ', 20) ('RR', 30) ('SS', 40) ('TT', 50) ('UU', 60) After changing value of specific key in Ordered Dict ('PP', 10) ('QQ', 111) ('RR', 30) ('SS', 40) ('TT', 50) ('UU', 60) Deleting and Reinserting the Elements in the OrderedDict classWhen we delete one element from the OrderedDict class and then perform the reinserting operation of that particular key and value, it will push that to the back. Although, OrderedDict class maintains the order during the process of insertion, but when the deletion process is performed, it removes the information of the ordering and treats the reinserted element as the new entry. Example: Output: OrderedDict: ('PP', 10) ('QQ', 20) ('RR', 30) ('SS', 40) ('TT', 50) ('UU', 60) After Deleting the key ('PP', 10) ('QQ', 20) ('SS', 40) ('TT', 50) ('UU', 60) After Re-inserting the key and value ('PP', 10) ('QQ', 20) ('SS', 40) ('TT', 50) ('UU', 60) ('RR', 30) How to Insert at the Beginning of the OrderedDictWhen the user wants to insert some element at the beginning of the OrderedDict class, he/she can use the 'update' method. Example: In the above code:
Output: The current dictionary values are : OrderedDict([('Jake', '10'), ('John', '20'), ('Ross', '40')]) The updated dictionary values are : OrderedDict([('Ryan', '70'), ('Jake', '10'), ('John', '20'), ('Ross', '40')]) How to Check the Order of Character in String by Using OrderedDictIf the user wants to check the order of the character in the string, they can use the 'OrderedDict' method. Let's understand the following example. Example: In the above code:
Output: The string is Hello Jake The input pattern is Ja The order of pattern is correct The string is Hello Jake The input pattern is ke The order of pattern is incorrect ConclusionIn this tutorial, we have discussed OrderedDict and how it is different from an ordinary dictionary. We have also explained how the users can delete and insert the elements in the OrderedDict and rearrange them at the beginning of the dictionary order. Next TopicT-Test in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India