OrderedDict in Python

OrderedDict in Python

The 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 Key

The 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 class

When 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 OrderedDict

When 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:

  • First, we have imported the required packages.
  • Then, we have created the Ordered Dictionary by using OrdereDict.
  • We used the ‘update’ method for specifying the key and its value.
  • Then, we used the ‘move_to_end’ function for moving the key and its value to the end.
  • The required output is displayed.

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 OrderedDict

If 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:

  • First, we imported the required packages.
  • Then, we defined the ‘check_order’ method, which will take two parameters.
  • We have then created the ordered dictionary by using the ‘fromkeys’ method.
  • We have initialized the length of the pattern to 0.
  • If the key is equal to the pattern, then the length of the pattern will be incremented.
  • But, if the length of the pattern is the same as the current length, this means the order is correct. Otherwise, the order is incorrect.

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

Conclusion

In 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.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263518.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论