Python OrderedDict क्या है? 🔥 | Collections OrderedDict in Hindi | Python Hindi
Python OrderedDict
Python में normal Dictionary अब (Python 3.7 से) खुद ही insertion order याद रखती है। लेकिन उससे पहले यह guarantee नहीं थी, और इसी जरूरत को पूरा करने के लिए Python के collections module में OrderedDict बनाया गया था — जो order को लेकर कुछ extra, खास features भी देता है।
अगर आपने अभी Python Dictionary in Hindi और Python Collections Module in Hindi नहीं पढ़े हैं, तो पहले वो articles पढ़ना उपयोगी रहेगा। Examples खुद run करने के लिए हमारा Online Python Editor & Compiler use कर सकते हैं।
Python OrderedDict क्या है?
OrderedDict Python के collections module का एक data structure है, जो normal dictionary जैसा ही काम करता है, लेकिन इसमें keys का insertion order (जिस क्रम में वे डाली गई थीं) पक्का याद रहता है, साथ ही order से जुड़े कुछ extra methods भी मिलते हैं।
सरल भाषा में — OrderedDict एक ऐसी dictionary है जो यह भी याद रखती है कि कौन सी key पहले आई थी।
Output:
OrderedDict को Import और Create करना
Output:
Key-Value Pairs Add करना
Output:
Values Access और Update करना
Values को access और update करना normal dictionary जैसा ही है।
Output:
ध्यान दें — "Math" की value update होने के बाद भी उसका original position (सबसे पहले) नहीं बदला।
New Items Add करना
नई key हमेशा OrderedDict के आखिर में जुड़ती है।
Output:
move_to_end()
move_to_end() किसी existing key को dictionary के आखिर में ले जाता है (या last=False देने पर शुरुआत में)।
Output:
"Math" अब सबसे आखिर में चला गया है।
Output:
popitem()
popitem() OrderedDict से एक item हटाता है और उसे return करता है — by default यह आखिरी item हटाता है (LIFO — Last In, First Out)।
Output:
popitem(last=False)
last=False देने पर popitem() सबसे पहला item हटाता है (FIFO — First In, First Out)।
Output:
| Method | काम |
|---|---|
move_to_end(key) | Key को आखिर में भेजता है |
move_to_end(key, last=False) | Key को शुरुआत में भेजता है |
popitem() | आखिरी item हटाता है |
popitem(last=False) | पहला item हटाता है |
Normal dict और OrderedDict का Basic Difference
| Feature | Normal dict | OrderedDict |
|---|---|---|
| Import चाहिए? | नहीं, built-in है | हाँ, collections से |
| Insertion Order | Python 3.7+ में maintain होता है | हमेशा maintain होता है, by design |
move_to_end() | उपलब्ध नहीं | उपलब्ध है |
popitem(last=...) | सिर्फ आखिरी item (कोई last parameter नहीं) | आखिरी या पहला, दोनों चुन सकते हैं |
Equality Check (==) | Order को ignore करता है | Order को भी compare करता है |
Output:
Normal dictionary में order मायने नहीं रखता (d1 == d2 True है), लेकिन OrderedDict में order भी check होता है, इसलिए od1 == od2 False आता है।
Practical Example — Recently Used Items
Output:
यह एक practical pattern है — हाल में देखे गए pages/items को track करना, और सबसे पुराने को आसानी से हटाना। यही idea LRU Cache जैसी systems में इस्तेमाल होता है।
Try it Yourself »Common Beginner Mistakes
Value को update करने मात्र से key का position नहीं बदलता — अगर position बदलनी है, तो move_to_end() खुद से इस्तेमाल करना पड़ेगा।
By default popitem() आखिरी item हटाता है। अगर पहला item चाहिए, तो explicitly last=False देना जरूरी है — वरना गलत item हट सकता है।
Python 3.7 के बाद normal dictionary भी insertion order maintain करती है, इसलिए अगर सिर्फ order चाहिए (और move_to_end() या order-sensitive equality जैसे extra features नहीं चाहिए), तो normal dictionary ही काफी है।
Exercise ?
किसी key को OrderedDict की शुरुआत में भेजने के लिए क्या इस्तेमाल होता है?
Coding Challenge
अब खुद अपने Online Python Editor में इन challenges को solve कीजिए:
- एक OrderedDict बनाइए जिसमें 4 students के नाम और उनके marks हों।
- किसी एक student को
move_to_end()से सबसे आखिर में भेजिए। - किसी दूसरे student को
move_to_end(key, last=False)से सबसे शुरुआत में भेजिए। popitem()औरpopitem(last=False)दोनों इस्तेमाल करके देखिए कि कौन सा item हटता है।- दो OrderedDicts बनाइए जिनमें same keys हों लेकिन अलग order में, और उन्हें
==से compare कीजिए।
Quick Revision
| Concept | Syntax |
|---|---|
| Import | from collections import OrderedDict |
| Create | OrderedDict() |
| Key को आखिर में भेजना | move_to_end(key) |
| Key को शुरुआत में भेजना | move_to_end(key, last=False) |
| आखिरी item हटाना | popitem() |
| पहला item हटाना | popitem(last=False) |
FAQ
Python में OrderedDict क्या है?
OrderedDict collections module का एक dictionary-जैसा data structure है, जो keys का insertion order पक्का याद रखता है और order से जुड़े extra methods भी देता है।
Normal dict और OrderedDict में अब (Python 3.7+ में) क्या फर्क बचा है?
दोनों insertion order maintain करते हैं, लेकिन OrderedDict में move_to_end() जैसे extra methods होते हैं, और इसकी equality check (==) order को भी ध्यान में रखती है, जबकि normal dict में नहीं।
move_to_end() क्या करता है?
move_to_end(key) किसी key को dictionary के आखिर में भेज देता है, और move_to_end(key, last=False) उसे शुरुआत में भेज देता है।
popitem() और popitem(last=False) में क्या फर्क है?
popitem() by default आखिरी item हटाता है, जबकि popitem(last=False) सबसे पहला item हटाता है।
OrderedDict अभी भी कब इस्तेमाल करना चाहिए?
जब आपको move_to_end() जैसी order-manipulation functionality चाहिए हो, या order-sensitive equality check चाहिए हो — जैसे LRU cache जैसी systems बनाते समय।
Summary
OrderedDict = एक dictionary, जो order को सिर्फ याद ही नहीं रखती, बल्कि उसे manage भी करने देती है।
इस article में आपने सीखा कि OrderedDict क्या है, इसे कैसे create करते हैं, keys कैसे add/update होती हैं, move_to_end() और popitem(last=...) कैसे काम करते हैं, और normal dictionary व OrderedDict में क्या फर्क है।
Modern Python में normal dictionary भी order maintain करती है, लेकिन जहाँ order को actively manage करना हो — जैसे recently-used items track करना — वहाँ OrderedDict आज भी बहुत उपयोगी है। अब ऊपर दिया गया coding challenge खुद अपने Online Editor में solve करें।

कोई टिप्पणी नहीं:
एक टिप्पणी भेजें