Python Collections क्या है? 🔥 | Collections Module in Hindi | Python Hindi
Python Collections Module
Python में हमने List, Tuple, Dictionary और Set जैसे built-in data structures देखे। लेकिन Python का collections module इन्हीं data structures के कुछ "upgraded" versions देता है, जो कुछ खास situations में normal data structures से ज्यादा सुविधाजनक होते हैं।
इस module में चार सबसे ज्यादा इस्तेमाल होने वाले tools हैं — Counter, defaultdict, deque और namedtuple। अगर आपने अभी Python Dictionary in Hindi और Python Lists Complete Guide in Hindi नहीं पढ़े हैं, तो पहले वो articles पढ़ना उपयोगी रहेगा। Examples खुद run करने के लिए हमारा Online Python Editor & Compiler use कर सकते हैं।
Python collections module क्या है?
collections Python का एक built-in module है जो List, Dictionary और Tuple जैसे normal data structures के specialized, ज्यादा सुविधाजनक versions provide करता है।
सरल भाषा में — collections module वही पुराने data structures हैं, बस कुछ extra superpowers के साथ।
Output:
Counter क्या है?
Counter किसी iterable (list, string, आदि) के हर element की कितनी बार repeat हुआ, यह गिनकर एक dictionary जैसा object बना देता है।
Counter से Frequency/Count निकालना
Output:
count[3] से पता चलता है कि 3 list में कुल 3 बार आया।
Counter का Practical Example
Output:
most_common(1) सबसे ज्यादा बार आने वाला element return करता है।
defaultdict क्या है?
Normal dictionary में अगर किसी missing key को access करें, तो Python KeyError देता है। defaultdict इस समस्या को हल करता है — missing key के लिए यह अपने-आप एक default value दे देता है।
Missing Key के लिए Default Value
Output:
चूंकि "Ankit" key मौजूद नहीं थी, defaultdict(int) ने अपने-आप default value 0 दे दी, बिना कोई error दिए।
defaultdict(int)
defaultdict(int) missing key के लिए default value 0 देता है — यह counting जैसे कामों के लिए बहुत उपयोगी है।
Output:
हर नई key को manually 0 से initialize करने की जरूरत नहीं पड़ी — defaultdict ने खुद यह संभाल लिया।
defaultdict(list)
defaultdict(list) missing key के लिए default value एक empty list देता है — यह grouping जैसे कामों के लिए उपयोगी है।
Output:
हर नई key को manually खाली list से शुरू करने की जरूरत नहीं पड़ी।
Try it Yourself »deque क्या है?
deque (double-ended queue) एक ऐसी list जैसी structure है जिसमें elements को दोनों सिरों (शुरुआत और आखिर) से जल्दी add या remove किया जा सकता है — normal list से भी तेज़।
Output:
append() और appendleft()
Output:
pop() और popleft()
Output:
| Method | काम |
|---|---|
append(value) | आखिर में element जोड़ता है |
appendleft(value) | शुरुआत में element जोड़ता है |
pop() | आखिर से element हटाता है |
popleft() | शुरुआत से element हटाता है |
Queue का Basic Example
deque का एक बहुत common इस्तेमाल Queue बनाने के लिए होता है, जहाँ सबसे पहले आया हुआ element सबसे पहले बाहर जाता है (FIFO — First In, First Out)।
Output:
नए customers append() से पीछे जुड़ते हैं, और popleft() से सबसे आगे वाला customer serve होकर निकलता है।
namedtuple क्या है?
namedtuple एक ऐसी Tuple है जिसके elements को index के साथ-साथ उनके नाम (field name) से भी access किया जा सकता है — इससे code ज्यादा readable बनता है।
Output:
Named Fields से Data Access करना
Output:
यहाँ s1.name से field name के through और s1[2] से normal index के through — दोनों तरीके से data access किया गया।
Counter, defaultdict, deque और namedtuple का Basic Comparison
| Tool | किस चीज़ का Upgrade है | मुख्य फायदा |
|---|---|---|
Counter | Dictionary | Elements की counting/frequency आसानी से निकालता है |
defaultdict | Dictionary | Missing key के लिए automatic default value देता है |
deque | List | दोनों सिरों से तेज़ add/remove करता है |
namedtuple | Tuple | Elements को नाम से access करने देता है |
Common Beginner Mistakes
deque को शुरुआत और आखिर से fast add/remove के लिए design किया गया है — बीच के elements को बार-बार index से access करना list जितना efficient नहीं होता।
namedtuple असल में एक Tuple ही है, इसलिए यह भी immutable होता है — इसे modify नहीं किया जा सकता।
Exercise ?
किसी list में elements की frequency (कितनी बार आए) निकालने के लिए कौन सा tool इस्तेमाल होता है?
Coding Challenge और Final Revision
अब खुद अपने Online Python Editor में इन challenges को solve कीजिए:
Counterका इस्तेमाल करके किसी sentence में हर word कितनी बार आया है, वह निकालिए।defaultdict(int)से किसी list में हर number की count निकालिए।defaultdict(list)से students को उनकी पसंदीदा subject के आधार पर group कीजिए।dequeसे एक simple queue बनाइए जिसमें 3 नाम add हों, फिर एक नामpopleft()से serve कीजिए।namedtupleसे एकProductबनाइए जिसमेंname,priceऔरquantityfields हों, और उसे field names से access कीजिए।
Quick Revision
| Tool | Syntax |
|---|---|
| Counter | Counter(iterable) |
| defaultdict | defaultdict(int) / defaultdict(list) |
| deque | deque([values]) |
| namedtuple | namedtuple('Name', ['field1', 'field2']) |
FAQ
Python में collections module क्या है?
collections Python का एक built-in module है जो List, Dictionary और Tuple जैसे data structures के specialized versions — जैसे Counter, defaultdict, deque, namedtuple — provide करता है।
Counter और normal Dictionary में क्या फर्क है?
Counter खासतौर पर elements की frequency (कितनी बार आए) count करने के लिए बना है और इसमें most_common() जैसे extra methods होते हैं, जो normal dictionary में नहीं होते।
defaultdict और normal Dictionary में क्या फर्क है?
Normal dictionary में missing key access करने पर KeyError आता है, जबकि defaultdict missing key के लिए अपने-आप एक default value (जैसे 0 या empty list) दे देता है।
deque किस काम आता है?
deque का इस्तेमाल तब होता है जब elements को list के दोनों सिरों (शुरुआत और आखिर) से बार-बार और जल्दी add या remove करना हो — जैसे queue बनाने के लिए।
namedtuple क्यों उपयोगी है?
namedtuple से Tuple के elements को index के साथ-साथ उनके नाम (field name) से भी access किया जा सकता है, जिससे code पढ़ने में आसान और self-explanatory बनता है।
Summary
collections module = List, Dictionary और Tuple के upgraded, ज्यादा सुविधाजनक versions।
इस article में आपने सीखा कि Python का collections module क्या है, Counter से elements की frequency कैसे निकालते हैं, defaultdict से missing key की समस्या कैसे हल होती है, deque से दोनों सिरों से fast operations कैसे करते हैं, और namedtuple से data को नाम के आधार पर access कैसे करते हैं।
ये चारों tools रोज़मर्रा की coding problems — counting, grouping, queues, और readable data structures — को आसान बना देते हैं। अब ऊपर दिया गया coding challenge खुद अपने Online Editor में solve करें।

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