Programming में कई बार हमें ऐसा Data Store करना होता है जिसमें प्रत्येक Value किसी विशेष पहचान (Key) से जुड़ी होती है। उदाहरण के लिए किसी विद्यार्थी का नाम, रोल नंबर, कक्षा और अंक; किसी कर्मचारी की ID, नाम और वेतन; या किसी Product का नाम, कीमत और Quantity।
यदि इस प्रकार के Data को List या Tuple में रखा जाए, तो किसी विशेष जानकारी को ढूँढना कठिन हो सकता है। ऐसी स्थिति में Dictionary सबसे उपयुक्त Data Type है।
Python में Dictionary एक Built-in Collection Data Type है, जो Key : Value Pair के रूप में Data Store करता है। प्रत्येक Key Unique होती है और उसी Key के माध्यम से संबंधित Value तक पहुँचा जाता है।
Dictionary Mutable होती है, अर्थात् इसमें नए Data जोड़े, बदले और हटाए जा सकते हैं।
Dictionary Python का एक Built-in Collection Data Type है जिसमें Data को Key : Value Pair के रूप में Curly Braces { } के अंदर लिखा जाता है।
dictionary_name = {
"key1": value1,
"key2": value2
}
student = {
"name": "Rahul",
"age": 20,
"course": "BCA"
}
- Data Key : Value Pair में Store होता है।
- प्रत्येक Key Unique होती है।
- Values Duplicate हो सकती हैं।
- Dictionary Mutable होती है।
- Dictionary Ordered होती है (Python 3.7+ से)।
- Indexing उपलब्ध नहीं होती।
- Key के माध्यम से Data Access किया जाता है।
- Nested Dictionary बनाई जा सकती है।
student = {
"name": "Rahul",
"age": 20
}
student = {}
student = dict(
name="Rahul",
age=20,
city="Lucknow"
)
student = dict([
("name", "Rahul"),
("age", 20)
])
Dictionary में विभिन्न Data Types
data = {
"name": "Rahul",
"age": 20,
"marks": 85.5,
"passed": True,
"subjects": ["Python", "C", "Java"]
}
student = {
"name":"Rahul",
"age":20,
"city":"Lucknow"
}
print(len(student))
Output
3
Dictionary के Elements Access करना
student = {
"name":"Rahul",
"age":20
}
print(student["name"])
Output
Rahul
print(student.get("age"))
Output
20
यदि Key मौजूद नहीं है, तो get() Error नहीं देता बल्कि None (या Default Value) लौटाता है।
student = {
"name":"Rahul"
}
student["city"] = "Lucknow"
print(student)
student = {
"age":20
}
student["age"] = 21
print(student)
student = {
"name":"Rahul",
"age":20
}
del student["age"]
print(student)
student = {
"name":"Rahul",
"age":20,
"city":"Lucknow"
}
for key in student:
print(key, student[key])
for key in student.keys():
print(key)
for value in student.values():
print(value)
Keys और Values दोनों प्राप्त करना
for key, value in student.items():
print(key, value)
student = {
"name":"Rahul",
"age":20
}
print("name" in student)
print("city" not in student)
students = {
"student1":{
"name":"Rahul",
"marks":90
},
"student2":{
"name":"Neha",
"marks":95
}
}
Access
print(students["student1"]["name"])
Output
Rahul
|
Function |
उपयोग |
|
len() |
कुल Key-Value Pair |
|
type() |
Data Type |
|
sorted() |
Keys को Sort करना |
|
max() |
सबसे बड़ी Key |
|
min() |
सबसे छोटी Key |
Python Dictionary के महत्वपूर्ण Methods—
- clear()
- copy()
- fromkeys()
- get()
- items()
- keys()
- values()
- pop()
- popitem()
- setdefault()
- update()
अगले अध्याय में इन सभी Methods को एक ही Master Guide में विस्तार से पढ़ेंगे।
Program 1 – Student Information
student = {
"name":"Rahul",
"age":20,
"marks":85
}
print(student)
student = {
"marks":80
}
student["marks"]=90
print(student)
Program 3 – Dictionary Traversal
student={
"name":"Rahul",
"city":"Lucknow"
}
for key,value in student.items():
print(key,value)
student={
"name":"Rahul",
"age":20
}
if "age" in student:
print("Key Found")
students={
"A":{
"marks":80
},
"B":{
"marks":90
}
}
print(students["B"]["marks"])
student={
"name":"Rahul"
}
print(student["city"])
❌ क्योंकि "city" Key मौजूद नहीं है।
- हमेशा Unique Keys का उपयोग करें।
- Unknown Key के लिए get() Method का उपयोग करें।
- Related Data को Dictionary में Store करें।
- Nested Data के लिए Nested Dictionary का उपयोग करें।
- यदि Data का क्रम महत्वपूर्ण हो, तो Python 3.7+ का उपयोग करें।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- Dictionary Key : Value Pair में Data Store करती है।
- Dictionary Mutable होती है।
- Keys Unique होती हैं।
- Values Duplicate हो सकती हैं।
- Dictionary में Indexing नहीं होती।
- get() KeyError से बचाता है।
- items() Key और Value दोनों Return करता है।
Frequently Asked Questions (FAQs)
Dictionary Python का Mutable Collection Data Type है जो Data को Key : Value Pair में Store करता है।
प्रश्न 2. क्या Dictionary में Duplicate Keys हो सकती हैं?
नहीं, प्रत्येक Key Unique होती है।
प्रश्न 3. क्या Duplicate Values रख सकते हैं?
हाँ।
प्रश्न 4. Dictionary में Indexing होती है?
नहीं, Data को Key के माध्यम से Access किया जाता है।
प्रश्न 5. get() Method का क्या लाभ है?
यदि Key मौजूद नहीं हो, तो KeyError नहीं आता।
- Dictionary = Key : Value Pair
- Curly Braces { }
- Mutable = ✔
- Keys = Unique
- Values = Duplicate Allowed
- Indexing = ✘
- Access = Key द्वारा
- Nested Dictionary = ✔
- get() = Safe Access
- items() = Keys + Values
Python Dictionary Methods in Hindi – get(), keys(), values(), items(), update(), pop(), popitem(), setdefault(), copy(), clear(), fromkeys() आदि सभी Dictionary Methods की Complete Guide। इसमें सभी Methods को एक ही Article में विस्तार से समझाया जाएगा।