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

🎥 Video में पूरा Topic समझें
Python Collections Module in Hindi

Python collections module क्या है?

collections Python का एक built-in module है जो List, Dictionary और Tuple जैसे normal data structures के specialized, ज्यादा सुविधाजनक versions provide करता है।

सरल भाषा में — collections module वही पुराने data structures हैं, बस कुछ extra superpowers के साथ।
Example
from collections import Counter fruits = ["apple", "banana", "apple", "mango", "banana", "apple"] count = Counter(fruits) print(count)

Output:

Counter({'apple': 3, 'banana': 2, 'mango': 1})
Try it Yourself »

Counter क्या है?

Counter किसी iterable (list, string, आदि) के हर element की कितनी बार repeat हुआ, यह गिनकर एक dictionary जैसा object बना देता है।

Syntax
from collections import Counter result = Counter(iterable)

Counter से Frequency/Count निकालना

Example
from collections import Counter numbers = [1, 2, 2, 3, 3, 3, 4] count = Counter(numbers) print(count) print(count[3])

Output:

Counter({3: 3, 2: 2, 1: 1, 4: 1}) 3

count[3] से पता चलता है कि 3 list में कुल 3 बार आया।

Try it Yourself »

Counter का Practical Example

Example — Letter Frequency
from collections import Counter word = "programming" letter_count = Counter(word) print(letter_count) print("Most Common:", letter_count.most_common(1))

Output:

Counter({'r': 2, 'g': 2, 'm': 2, 'p': 1, 'o': 1, 'a': 1, 'i': 1, 'n': 1}) Most Common: [('r', 2)]

most_common(1) सबसे ज्यादा बार आने वाला element return करता है।

Try it Yourself »

defaultdict क्या है?

Normal dictionary में अगर किसी missing key को access करें, तो Python KeyError देता है। defaultdict इस समस्या को हल करता है — missing key के लिए यह अपने-आप एक default value दे देता है।

Syntax
from collections import defaultdict my_dict = defaultdict(default_type)

Missing Key के लिए Default Value

Normal Dictionary — यह Error देगा
normal_dict = {} print(normal_dict["marks"]) # KeyError
defaultdict — Error नहीं देगा
from collections import defaultdict marks = defaultdict(int) print(marks["Ankit"])

Output:

0

चूंकि "Ankit" key मौजूद नहीं थी, defaultdict(int) ने अपने-आप default value 0 दे दी, बिना कोई error दिए।

Try it Yourself »

defaultdict(int)

defaultdict(int) missing key के लिए default value 0 देता है — यह counting जैसे कामों के लिए बहुत उपयोगी है।

Example — Word Count
from collections import defaultdict words = ["apple", "banana", "apple", "mango", "apple"] count = defaultdict(int) for word in words: count[word] += 1 print(count)

Output:

defaultdict(<class 'int'>, {'apple': 3, 'banana': 1, 'mango': 1})

हर नई key को manually 0 से initialize करने की जरूरत नहीं पड़ी — defaultdict ने खुद यह संभाल लिया।

Try it Yourself »

defaultdict(list)

defaultdict(list) missing key के लिए default value एक empty list देता है — यह grouping जैसे कामों के लिए उपयोगी है।

Example — Grouping Students by Grade
from collections import defaultdict grades = defaultdict(list) grades["A"].append("Ankit") grades["B"].append("Rahul") grades["A"].append("Priya") print(grades)

Output:

defaultdict(<class 'list'>, {'A': ['Ankit', 'Priya'], 'B': ['Rahul']})

हर नई key को manually खाली list से शुरू करने की जरूरत नहीं पड़ी।

Try it Yourself »

deque क्या है?

deque (double-ended queue) एक ऐसी list जैसी structure है जिसमें elements को दोनों सिरों (शुरुआत और आखिर) से जल्दी add या remove किया जा सकता है — normal list से भी तेज़।

Example
from collections import deque d = deque([1, 2, 3]) print(d)

Output:

deque([1, 2, 3])
Try it Yourself »

append() और appendleft()

Example
from collections import deque d = deque([1, 2, 3]) d.append(4) # आखिर में जोड़ना d.appendleft(0) # शुरुआत में जोड़ना print(d)

Output:

deque([0, 1, 2, 3, 4])
Try it Yourself »

pop() और popleft()

Example
from collections import deque d = deque([1, 2, 3, 4]) d.pop() # आखिर से हटाना d.popleft() # शुरुआत से हटाना print(d)

Output:

deque([2, 3])
Try it Yourself »
Methodकाम
append(value)आखिर में element जोड़ता है
appendleft(value)शुरुआत में element जोड़ता है
pop()आखिर से element हटाता है
popleft()शुरुआत से element हटाता है

Queue का Basic Example

deque का एक बहुत common इस्तेमाल Queue बनाने के लिए होता है, जहाँ सबसे पहले आया हुआ element सबसे पहले बाहर जाता है (FIFO — First In, First Out)।

Example — Customer Queue
from collections import deque queue = deque() queue.append("Customer 1") queue.append("Customer 2") queue.append("Customer 3") print("Queue:", queue) served = queue.popleft() print("Served:", served) print("Remaining Queue:", queue)

Output:

Queue: deque(['Customer 1', 'Customer 2', 'Customer 3']) Served: Customer 1 Remaining Queue: deque(['Customer 2', 'Customer 3'])

नए customers append() से पीछे जुड़ते हैं, और popleft() से सबसे आगे वाला customer serve होकर निकलता है।

Try it Yourself »

namedtuple क्या है?

namedtuple एक ऐसी Tuple है जिसके elements को index के साथ-साथ उनके नाम (field name) से भी access किया जा सकता है — इससे code ज्यादा readable बनता है।

Syntax
from collections import namedtuple TypeName = namedtuple('TypeName', ['field1', 'field2'])
Example
from collections import namedtuple Student = namedtuple('Student', ['name', 'age', 'city']) s1 = Student("Ankit", 25, "Lucknow") print(s1)

Output:

Student(name='Ankit', age=25, city='Lucknow')
Try it Yourself »

Named Fields से Data Access करना

Example
from collections import namedtuple Student = namedtuple('Student', ['name', 'age', 'city']) s1 = Student("Ankit", 25, "Lucknow") print(s1.name) print(s1.age) print(s1[2])

Output:

Ankit 25 Lucknow

यहाँ s1.name से field name के through और s1[2] से normal index के through — दोनों तरीके से data access किया गया।

Try it Yourself »

Counter, defaultdict, deque और namedtuple का Basic Comparison

Toolकिस चीज़ का Upgrade हैमुख्य फायदा
CounterDictionaryElements की counting/frequency आसानी से निकालता है
defaultdictDictionaryMissing key के लिए automatic default value देता है
dequeListदोनों सिरों से तेज़ add/remove करता है
namedtupleTupleElements को नाम से access करने देता है

Common Beginner Mistakes

❌ बिना import किए इस्तेमाल करना
count = Counter([1, 2, 2]) # NameError
✅ सही तरीका
from collections import Counter count = Counter([1, 2, 2])
❌ defaultdict को बिना Type दिए बनाना
my_dict = defaultdict() # TypeError जब missing key access हो
✅ सही तरीका
my_dict = defaultdict(int) # या list, str, आदि
❌ deque को Normal List जैसे index से बहुत ज्यादा access करना

deque को शुरुआत और आखिर से fast add/remove के लिए design किया गया है — बीच के elements को बार-बार index से access करना list जितना efficient नहीं होता।

❌ namedtuple को Modify करने की कोशिश
s1 = Student("Ankit", 25, "Lucknow") s1.age = 26 # AttributeError

namedtuple असल में एक Tuple ही है, इसलिए यह भी immutable होता है — इसे modify नहीं किया जा सकता।

Exercise ?

किसी list में elements की frequency (कितनी बार आए) निकालने के लिए कौन सा tool इस्तेमाल होता है?

deque
Counter
namedtuple

Coding Challenge और Final Revision

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

  1. Counter का इस्तेमाल करके किसी sentence में हर word कितनी बार आया है, वह निकालिए।
  2. defaultdict(int) से किसी list में हर number की count निकालिए।
  3. defaultdict(list) से students को उनकी पसंदीदा subject के आधार पर group कीजिए।
  4. deque से एक simple queue बनाइए जिसमें 3 नाम add हों, फिर एक नाम popleft() से serve कीजिए।
  5. namedtuple से एक Product बनाइए जिसमें name, price और quantity fields हों, और उसे field names से access कीजिए।

Quick Revision

ToolSyntax
CounterCounter(iterable)
defaultdictdefaultdict(int) / defaultdict(list)
dequedeque([values])
namedtuplenamedtuple('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 करें।

Related Articles

🧑‍💻 Python Coding Lab

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