जब आदेशित शब्दकोश की शुरुआत में तत्वों को सम्मिलित करने की आवश्यकता होती है, तो 'अपडेट' पद्धति का उपयोग किया जा सकता है।
नीचे उसी का प्रदर्शन है -
उदाहरण
from collections import OrderedDict
my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
print("The dictionary is :")
print(my_ordered_dict)
my_ordered_dict.update({'Mark':'7'})
my_ordered_dict.move_to_end('Mark', last = False)
print("The resultant dictionary is : ")
print(my_ordered_dict) आउटपुट
The dictionary is :
OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
The resultant dictionary is :
OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')]) स्पष्टीकरण
-
आवश्यक पैकेज आयात किए जाते हैं।
-
OrderedDict' का उपयोग करके एक आदेशित शब्दकोश बनाया जाता है।
-
यह कंसोल पर प्रदर्शित होता है।
-
कुंजी और मान निर्दिष्ट करने के लिए 'अपडेट' पद्धति का उपयोग किया जाता है।
-
'move_to_end' मेथड का इस्तेमाल की वैल्यू पेयर को अंत तक ले जाने के लिए किया जाता है।
-
आउटपुट कंसोल पर प्रदर्शित होता है।