Table of Contents
2. Characteristics of Dictionary in Hindi – डिक्शनरी की विशेषताएं
4. Accessing Dictionary Elements in Hindi – एलिमेंट्स कैसे Access करें
5. Dictionary Methods and Operations in Hindi – मेथड्स और ऑपरेशन्स
7. Advantages of Dictionary in Python in Hindi – डिक्शनरी के फायदे
Dictionary in Python in Hindi – डिक्शनरी क्या है?
Dictionary Python का एक built-in data type है जिसमें data को key-value pairs के रूप में store किया जाता है।
आसान भाषा में कहें तो, जैसे एक असली dictionary में किसी word (key) का meaning (value) होता है, वैसे ही Python dictionary में हर key के साथ एक value जुड़ी होती है।
Dictionary को curly braces { } के अंदर लिखा जाता है, और हर key-value pair को colon (:) से अलग किया जाता है।
List और tuple की तरह dictionary index number से नहीं, बल्कि key के through data access करती है, जिससे searching बहुत तेज हो जाती है।
Dictionary एक mutable data type है, यानी इसे बनाने के बाद इसमें values को add, update या delete किया जा सकता है।
एक dictionary में हर key unique होनी चाहिए, यानी एक ही key दो बार नहीं हो सकती, लेकिन values duplicate हो सकती हैं।
Dictionary का इस्तेमाल तब सबसे ज्यादा किया जाता है जब data को किसी identifier के आधार पर organize करना हो, जैसे student का roll number और उसका नाम।
Python में dictionary को dict() function से भी बनाया जा सकता है, curly braces के अलावा।
Real-world programming में dictionary का इस्तेमाल JSON data, configuration settings और database जैसी records को represent करने के लिए बहुत होता है।
Dictionary, list और tuple से इस मायने में अलग है कि इसमें data unordered (Python 3.7 से पहले) या insertion order में (3.7 के बाद) store होता है, लेकिन access हमेशा key के through होता है।
Example of Dictionary – एक Simple शुरुआत
मान लो आपको किसी student की details store करनी हैं जैसे नाम, उम्र और class। इसके लिए dictionary सबसे सही data type है।
student = {
"name": "Rahul",
"age": 20,
"class": "12th"
}
print(student)
# Output: {'name': 'Rahul', 'age': 20, 'class': '12th'}
Characteristics of Dictionary in Hindi – डिक्शनरी की विशेषताएं
1. Key-Value Pair Structure
Dictionary में data हमेशा key और value के जोड़े में store होता है। हर value को उसकी key के through ही access किया जा सकता है।
2. Mutable Nature
Dictionary एक mutable data type है, यानी बनने के बाद भी इसमें नई keys add की जा सकती हैं या existing values को बदला जा सकता है।
3. Unique Keys
Dictionary में हर key unique होनी चाहिए। अगर एक जैसी key दो बार दी जाए, तो पुरानी value automatically नई value से replace हो जाती है।
4. Ordered (Python 3.7+)
Python 3.7 और उसके बाद के versions में dictionary अपने elements को उसी order में रखती है जिस order में वे insert किए गए थे।
5. Dynamic Size
Dictionary का size fixed नहीं होता, जरूरत के हिसाब से इसमें keys और values को कभी भी add या remove किया जा सकता है।
6. Heterogeneous Values
एक dictionary में अलग-अलग data types की values एक साथ रखी जा सकती हैं, जैसे string, integer, list या यहाँ तक कि दूसरी dictionary भी।
यह SVG दिखा रहा है कि dictionary में हर key अपनी value से किस तरह जुड़ी होती है।
Creating a Dictionary in Hindi – डिक्शनरी कैसे बनाएं
1. Curly Braces से Dictionary बनाना
सबसे common तरीका है curly braces { } के अंदर key-value pairs लिखना।
fruit_prices = {
"apple": 120,
"banana": 40,
"mango": 90
}
print(fruit_prices)
2. dict() Function से Dictionary बनाना
dict() function का इस्तेमाल करके भी dictionary बनाई जा सकती है, यह तरीका खासकर तब useful होता है जब keys simple strings हों।
student = dict(name="Anjali", age=21, city="Bhopal")
print(student)
# Output: {'name': 'Anjali', 'age': 21, 'city': 'Bhopal'}
3. Empty Dictionary बनाना
अगर शुरुआत में dictionary खाली रखनी हो और बाद में values add करनी हों, तो सिर्फ curly braces से empty dictionary बनाई जा सकती है।
empty_dict = {}
print(type(empty_dict))
# Output: <class 'dict'>
4. Nested Dictionary बनाना
एक dictionary के अंदर दूसरी dictionary भी रखी जा सकती है, जिसे nested dictionary कहते हैं। यह complex data structure बनाने में काम आता है।
students = {
"student1": {"name": "Riya", "age": 19},
"student2": {"name": "Aman", "age": 22}
}
print(students["student1"]["name"])
# Output: Riya
Accessing Dictionary Elements in Hindi – एलिमेंट्स कैसे Access करें
1. Square Brackets से Access करना
Dictionary की value को उसकी key के through square brackets [ ] में access किया जाता है।
student = {"name": "Karan", "age": 23}
print(student["name"])
# Output: Karan
2. get() Method से Access करना
get() method का इस्तेमाल करना ज्यादा safe होता है, क्योंकि अगर key exist नहीं करती तो यह error नहीं देता बल्कि None या दी गई default value return करता है।
student = {"name": "Karan", "age": 23}
print(student.get("city"))
# Output: None
print(student.get("city", "Not Available"))
# Output: Not Available
3. keys(), values() और items() से Access करना
पूरी dictionary की सभी keys, values या दोनों को एक साथ access करने के लिए ये तीन methods इस्तेमाल होते हैं।
student = {"name": "Karan", "age": 23, "city": "Indore"}
print(student.keys())
print(student.values())
print(student.items())
4. Loop के जरिए Dictionary Access करना
for loop की मदद से dictionary के सभी key-value pairs पर एक साथ iterate किया जा सकता है।
student = {"name": "Karan", "age": 23, "city": "Indore"}
for key, value in student.items():
print(key, ":", value)
Dictionary Methods and Operations in Hindi – मेथड्स और ऑपरेशन्स
1. update():
इससे dictionary में एक साथ कई keys और values update या add की जा सकती हैं।2. pop():
यह किसी specific key को उसकी value के साथ dictionary से remove कर देता है और उस value को return भी करता है।3. popitem():
यह dictionary का सबसे आखिरी key-value pair remove करके return करता है।4. clear():
यह dictionary के सारे elements को हटाकर उसे पूरी तरह खाली कर देता है।5. copy():
यह dictionary की एक अलग copy बनाता है, ताकि original dictionary में बदलाव किए बिना उस copy पर काम किया जा सके।6. len():
यह dictionary में मौजूद कुल key-value pairs की संख्या बताता है।
student = {"name": "Karan", "age": 23}
student.update({"city": "Indore"})
print(student)
# Output: {'name': 'Karan', 'age': 23, 'city': 'Indore'}
student.pop("age")
print(student)
# Output: {'name': 'Karan', 'city': 'Indore'}
print(len(student))
# Output: 2
Key की Existence Check करना
in keyword की मदद से यह पता लगाया जा सकता है कि कोई particular key dictionary में मौजूद है या नहीं।
student = {"name": "Karan", "age": 23}
print("name" in student)
# Output: True
print("city" in student)
# Output: False
यह SVG dictionary के कुछ सबसे ज्यादा इस्तेमाल होने वाले methods को दिखा रहा है।
Applications of Dictionary in Hindi – डिक्शनरी के उपयोग
1. Student Record System बनाना
Dictionary का उपयोग student का नाम, roll number, marks जैसी details को एक साथ organize करने के लिए किया जाता है।
2. JSON Data Handle करना
Web development और API के काम में JSON data को Python dictionary की तरह ही represent किया जाता है, इसलिए dictionary की समझ बहुत जरूरी हो जाती है।
3. Words या Items की Frequency Count करना
किसी text में हर word कितनी बार आया है, या किसी list में हर item कितनी बार repeat हुआ है, यह count करने के लिए dictionary सबसे उपयोगी data type है।
text = "apple banana apple mango banana apple"
words = text.split()
frequency = {}
for word in words:
frequency[word] = frequency.get(word, 0) + 1
print(frequency)
# Output: {'apple': 3, 'banana': 2, 'mango': 1}
4. Database जैसी Structure बनाना
छोटे programs में dictionary को एक mini-database की तरह इस्तेमाल किया जाता है, जहाँ हर unique key एक record की तरह काम करती है।
5. Configuration Settings Store करना
Software applications में settings या preferences को अक्सर key-value pairs में store किया जाता है, जिसके लिए dictionary एकदम सही data type है।
Advantages of Dictionary in Python in Hindi – डिक्शनरी के फायदे
Data को key के through बहुत तेजी से access किया जा सकता है, index याद रखने की जरूरत नहीं पड़ती।
Dictionary mutable होती है, इसलिए इसमें values को आसानी से add, update या delete किया जा सकता है।
अलग-अलग data types की values को एक साथ store किया जा सकता है।
Real-world data (जैसे JSON) को represent करना बहुत आसान हो जाता है।
Large data को organize और search करना list के मुकाबले ज्यादा efficient होता है।
Nested dictionaries की मदद से complex और structured data आसानी से बनाया जा सकता है।
Disadvantages of Dictionary in Hindi – डिक्शनरी की सीमाएं
हर key unique होनी चाहिए, duplicate keys allow नहीं होतीं।
List के मुकाबले dictionary थोड़ी ज्यादा memory इस्तेमाल करती है।
Keys के लिए सिर्फ immutable data types (जैसे string, number, tuple) ही इस्तेमाल की जा सकती हैं।
Beginners को शुरुआत में key-value structure समझने में थोड़ी दिक्कत हो सकती है।