Seedha content par jao
🤖 Python Hindi Bot
Namaste! Main Python Hindi Bot hoon 🐍
Python ya AI se related kuch bhi type karke pucho, ya neeche se quick sawaal chuno.
WhatsApp

Stay Connected with PythonHindii

Join our WhatsApp Community to get the latest Python tutorials, coding tips, interview questions, project updates, and learning resources directly on your phone.

Join WhatsApp Community
⇧
Python Hindi Logo
Python in Hindi | Programming Education

Python OrderedDict क्या है? 🔥 | Collections OrderedDict in Hindi | Python Hindi

Python OrderedDict in Hindi - Collections OrderedDict

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 कर सकते हैं।

🎥 Video में पूरा Topic समझें

Python OrderedDict क्या है?

OrderedDict Python के collections module का एक data structure है, जो normal dictionary जैसा ही काम करता है, लेकिन इसमें keys का insertion order (जिस क्रम में वे डाली गई थीं) पक्का याद रहता है, साथ ही order से जुड़े कुछ extra methods भी मिलते हैं।

सरल भाषा में — OrderedDict एक ऐसी dictionary है जो यह भी याद रखती है कि कौन सी key पहले आई थी।
Example
from collections import OrderedDict student = OrderedDict() student["name"] = "Ankit" student["age"] = 25 student["city"] = "Lucknow" print(student)

Output:

OrderedDict([('name', 'Ankit'), ('age', 25), ('city', 'Lucknow')])
Try it Yourself »

OrderedDict को Import और Create करना

Syntax
from collections import OrderedDict my_dict = OrderedDict()
Example — Existing Data से बनाना
from collections import OrderedDict colors = OrderedDict([("red", 1), ("green", 2), ("blue", 3)]) print(colors)

Output:

OrderedDict([('red', 1), ('green', 2), ('blue', 3)])
Try it Yourself »

Key-Value Pairs Add करना

Example
from collections import OrderedDict marks = OrderedDict() marks["Math"] = 85 marks["Science"] = 90 marks["English"] = 78 print(marks)

Output:

OrderedDict([('Math', 85), ('Science', 90), ('English', 78)])
Try it Yourself »

Values Access और Update करना

Values को access और update करना normal dictionary जैसा ही है।

Example
from collections import OrderedDict marks = OrderedDict() marks["Math"] = 85 marks["Science"] = 90 print(marks["Math"]) marks["Math"] = 95 print(marks)

Output:

85 OrderedDict([('Math', 95), ('Science', 90)])

ध्यान दें — "Math" की value update होने के बाद भी उसका original position (सबसे पहले) नहीं बदला।

Try it Yourself »

New Items Add करना

नई key हमेशा OrderedDict के आखिर में जुड़ती है।

Example
from collections import OrderedDict marks = OrderedDict() marks["Math"] = 85 marks["Science"] = 90 marks["English"] = 78 print(marks)

Output:

OrderedDict([('Math', 85), ('Science', 90), ('English', 78)])
Try it Yourself »

move_to_end()

move_to_end() किसी existing key को dictionary के आखिर में ले जाता है (या last=False देने पर शुरुआत में)।

Example — आखिर में Move करना
from collections import OrderedDict marks = OrderedDict() marks["Math"] = 85 marks["Science"] = 90 marks["English"] = 78 marks.move_to_end("Math") print(marks)

Output:

OrderedDict([('Science', 90), ('English', 78), ('Math', 85)])

"Math" अब सबसे आखिर में चला गया है।

Try it Yourself »
Example — शुरुआत में Move करना
from collections import OrderedDict marks = OrderedDict() marks["Math"] = 85 marks["Science"] = 90 marks["English"] = 78 marks.move_to_end("English", last=False) print(marks)

Output:

OrderedDict([('English', 78), ('Math', 85), ('Science', 90)])
Try it Yourself »

popitem()

popitem() OrderedDict से एक item हटाता है और उसे return करता है — by default यह आखिरी item हटाता है (LIFO — Last In, First Out)।

Example
from collections import OrderedDict marks = OrderedDict() marks["Math"] = 85 marks["Science"] = 90 marks["English"] = 78 removed = marks.popitem() print("Removed:", removed) print("Remaining:", marks)

Output:

Removed: ('English', 78) Remaining: OrderedDict([('Math', 85), ('Science', 90)])
Try it Yourself »

popitem(last=False)

last=False देने पर popitem() सबसे पहला item हटाता है (FIFO — First In, First Out)।

Example
from collections import OrderedDict marks = OrderedDict() marks["Math"] = 85 marks["Science"] = 90 marks["English"] = 78 removed = marks.popitem(last=False) print("Removed:", removed) print("Remaining:", marks)

Output:

Removed: ('Math', 85) Remaining: OrderedDict([('Science', 90), ('English', 78)])
Try it Yourself »
Methodकाम
move_to_end(key)Key को आखिर में भेजता है
move_to_end(key, last=False)Key को शुरुआत में भेजता है
popitem()आखिरी item हटाता है
popitem(last=False)पहला item हटाता है

Normal dict और OrderedDict का Basic Difference

FeatureNormal dictOrderedDict
Import चाहिए?नहीं, built-in हैहाँ, collections से
Insertion OrderPython 3.7+ में maintain होता हैहमेशा maintain होता है, by design
move_to_end()उपलब्ध नहींउपलब्ध है
popitem(last=...)सिर्फ आखिरी item (कोई last parameter नहीं)आखिरी या पहला, दोनों चुन सकते हैं
Equality Check (==)Order को ignore करता हैOrder को भी compare करता है
Example — Equality में Order का फर्क
from collections import OrderedDict d1 = {"a": 1, "b": 2} d2 = {"b": 2, "a": 1} od1 = OrderedDict([("a", 1), ("b", 2)]) od2 = OrderedDict([("b", 2), ("a", 1)]) print(d1 == d2) print(od1 == od2)

Output:

True False

Normal dictionary में order मायने नहीं रखता (d1 == d2 True है), लेकिन OrderedDict में order भी check होता है, इसलिए od1 == od2 False आता है।

Try it Yourself »

Practical Example — Recently Used Items

Example — LRU जैसा Pattern
from collections import OrderedDict recent_pages = OrderedDict() recent_pages["Home"] = True recent_pages["Courses"] = True recent_pages["Profile"] = True # User ने फिर से "Home" visit किया recent_pages.move_to_end("Home") print("Order after revisit:", recent_pages) # सबसे पुराना page हटाना (जो सबसे कम हाल में visit हुआ) oldest = recent_pages.popitem(last=False) print("Removed oldest:", oldest) print("Final:", recent_pages)

Output:

Order after revisit: OrderedDict([('Courses', True), ('Profile', True), ('Home', True)]) Removed oldest: ('Courses', True) Final: OrderedDict([('Profile', True), ('Home', True)])

यह एक practical pattern है — हाल में देखे गए pages/items को track करना, और सबसे पुराने को आसानी से हटाना। यही idea LRU Cache जैसी systems में इस्तेमाल होता है।

Try it Yourself »

Common Beginner Mistakes

❌ बिना import किए OrderedDict इस्तेमाल करना
d = OrderedDict() # NameError
✅ सही तरीका
from collections import OrderedDict d = OrderedDict()
❌ यह सोचना कि Value Update करने से Position बदल जाएगी
marks["Math"] = 95 # Position वही रहेगी, आखिर में नहीं जाएगा

Value को update करने मात्र से key का position नहीं बदलता — अगर position बदलनी है, तो move_to_end() खुद से इस्तेमाल करना पड़ेगा।

❌ popitem(last=False) को popitem() जैसा ही समझना

By default popitem() आखिरी item हटाता है। अगर पहला item चाहिए, तो explicitly last=False देना जरूरी है — वरना गलत item हट सकता है।

❌ Modern Python में हर जगह OrderedDict इस्तेमाल करना

Python 3.7 के बाद normal dictionary भी insertion order maintain करती है, इसलिए अगर सिर्फ order चाहिए (और move_to_end() या order-sensitive equality जैसे extra features नहीं चाहिए), तो normal dictionary ही काफी है।

Exercise ?

किसी key को OrderedDict की शुरुआत में भेजने के लिए क्या इस्तेमाल होता है?

move_to_end(key)
move_to_end(key, last=False)
popitem(last=False)

Coding Challenge

अब खुद अपने Online Python Editor में इन challenges को solve कीजिए:

  1. एक OrderedDict बनाइए जिसमें 4 students के नाम और उनके marks हों।
  2. किसी एक student को move_to_end() से सबसे आखिर में भेजिए।
  3. किसी दूसरे student को move_to_end(key, last=False) से सबसे शुरुआत में भेजिए।
  4. popitem() और popitem(last=False) दोनों इस्तेमाल करके देखिए कि कौन सा item हटता है।
  5. दो OrderedDicts बनाइए जिनमें same keys हों लेकिन अलग order में, और उन्हें == से compare कीजिए।

Quick Revision

ConceptSyntax
Importfrom collections import OrderedDict
CreateOrderedDict()
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 करें।

Related Articles

🧑‍💻 Python Coding Lab

Python code likho, run karo aur practice karo — directly browser mein.