Python में Dictionary एक शक्तिशाली Collection Data Type है, जिसमें Data को Key : Value Pair के रूप में रखा जाता है। Dictionary बनने के बाद उसमें नई Entries जोड़ना, पुरानी Values बदलना, किसी Key की Value प्राप्त करना, Data हटाना, पूरी Dictionary साफ़ करना या उसकी Copy बनाना जैसे कार्य बार-बार करने पड़ते हैं।
इन सभी कार्यों के लिए Python कई Built-in Dictionary Methods प्रदान करता है।
इस अध्याय में Dictionary के सभी महत्वपूर्ण Methods को Syntax, Examples, Output, Practical Programs और परीक्षा की दृष्टि से महत्वपूर्ण तथ्यों सहित विस्तार से समझेंगे।
Dictionary Methods वे Built-in Functions हैं जो केवल Dictionary Object पर कार्य करते हैं।
dictionary.method_name(arguments)
उदाहरण
student = {
"name": "Rahul"
}
print(student.get("name"))
Output
Rahul
Python Dictionary Methods की सूची
|
क्रम |
Method |
उपयोग |
|
1 |
get() |
Key की Value प्राप्त करना |
|
2 |
keys() |
सभी Keys प्राप्त करना |
|
3 |
values() |
सभी Values प्राप्त करना |
|
4 |
items() |
सभी Key-Value Pair प्राप्त करना |
|
5 |
update() |
Dictionary Update करना |
|
6 |
pop() |
किसी Key को हटाना |
|
7 |
popitem() |
अंतिम Key-Value Pair हटाना |
|
8 |
setdefault() |
Default Value Set करना |
|
9 |
copy() |
Dictionary की Copy बनाना |
|
10 |
clear() |
पूरी Dictionary खाली करना |
|
11 |
fromkeys() |
Keys से नई Dictionary बनाना |
किसी Key की Value प्राप्त करने के लिए उपयोग किया जाता है।
dictionary.get(key, default_value)
student = {
"name": "Rahul",
"age": 20
}
print(student.get("name"))
Output
Rahul
यदि Key मौजूद नहीं हो:
print(student.get("city"))
Output
None
Default Value के साथ:
print(student.get("city", "Not Available"))
Output
Not Available
Dictionary की सभी Keys प्राप्त करने के लिए।
student = {
"name":"Rahul",
"age":20
}
print(student.keys())
Output
dict_keys(['name', 'age'])
Dictionary की सभी Values प्राप्त करने के लिए।
print(student.values())
Output
dict_values(['Rahul', 20])
सभी Key-Value Pair प्राप्त करने के लिए।
print(student.items())
Output
dict_items([('name', 'Rahul'), ('age', 20)])
Loop में उपयोग:
for key, value in student.items():
print(key, value)
नई Key जोड़ने या पुरानी Value बदलने के लिए।
student = {
"name":"Rahul"
}
student.update({"city":"Lucknow"})
print(student)
Output
{'name':'Rahul','city':'Lucknow'}
Value Update:
student.update({"name":"Amit"})
किसी विशेष Key को हटाने के लिए।
dictionary.pop(key)
student = {
"name":"Rahul",
"age":20
}
student.pop("age")
print(student)
Output
{'name':'Rahul'}
Dictionary का अंतिम Key-Value Pair हटाता है।
student = {
"name":"Rahul",
"age":20
}
student.popitem()
print(student)
यदि Key मौजूद हो तो उसकी Value लौटाता है, अन्यथा नई Key जोड़ देता है।
student = {
"name":"Rahul"
}
student.setdefault("city","Lucknow")
print(student)
Output
{'name':'Rahul','city':'Lucknow'}
Dictionary की अलग Copy बनाता है।
student = {
"name":"Rahul"
}
new_student = student.copy()
print(new_student)
a = {"x":10}
b = a
b["x"] = 100
print(a)
Output
{'x':100}
a = {"x":10}
b = a.copy()
b["x"] = 100
print(a)
print(b)
Output
{'x':10}
{'x':100}
पूरी Dictionary खाली कर देता है।
student = {
"name":"Rahul",
"age":20
}
student.clear()
print(student)
Output
{}
Keys की सहायता से नई Dictionary बनाता है।
keys = ("name","age","city")
student = dict.fromkeys(keys)
print(student)
Output
{'name':None,'age':None,'city':None}
Default Value के साथ
student = dict.fromkeys(keys,0)
print(student)
Output
{'name':0,'age':0,'city':0}
|
Method |
Return Value |
Original Dictionary बदलती है? |
|
get() |
Value |
✘ |
|
keys() |
View Object |
✘ |
|
values() |
View Object |
✘ |
|
items() |
View Object |
✘ |
|
update() |
None |
✔ |
|
pop() |
Value |
✔ |
|
popitem() |
Key-Value Pair |
✔ |
|
setdefault() |
Value |
✔ (यदि Key न हो) |
|
copy() |
नई Dictionary |
✘ |
|
clear() |
None |
✔ |
|
fromkeys() |
नई Dictionary |
✘ |
student = {}
student["name"] = "Rahul"
student["marks"] = 95
print(student)
student = {
"name":"Rahul"
}
print(student.get("city","City Not Found"))
Program 3 – Dictionary Traversal
student = {
"name":"Rahul",
"age":20
}
for key,value in student.items():
print(key,value)
student = {
"name":"Rahul"
}
student.update({"age":20})
print(student)
student = {
"name":"Rahul",
"city":"Lucknow"
}
student.pop("city")
print(student)
student = {
"name":"Rahul"
}
student.pop("city")
❌ क्योंकि "city" Key मौजूद नहीं है।
- Unknown Key के लिए get() का उपयोग करें।
- Loop में items() सबसे उपयोगी Method है।
- Dictionary Merge करने के लिए update() उपयोग करें।
- Original Data सुरक्षित रखना हो तो copy() का उपयोग करें।
- Temporary Data हटाने के लिए pop() उपयोग करें।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- get() KeyError से बचाता है।
- keys() सभी Keys Return करता है।
- values() सभी Values Return करता है।
- items() Key और Value दोनों Return करता है।
- update() नई Entries जोड़ता या पुरानी बदलता है।
- pop() विशेष Key हटाता है।
- popitem() अंतिम Key-Value Pair हटाता है।
- copy() नई Dictionary बनाता है।
- clear() पूरी Dictionary खाली कर देता है।
- fromkeys() नई Dictionary बनाने के लिए उपयोग होता है।
Frequently Asked Questions (FAQs)
प्रश्न 1. get() और Square Bracket ([]) में क्या अंतर है?
get() Key न मिलने पर Error नहीं देता, जबकि [] से KeyError आता है।
प्रश्न 2. update() का उपयोग क्यों किया जाता है?
नई Key जोड़ने और पुरानी Value बदलने के लिए।
प्रश्न 3. items() क्या Return करता है?
सभी Key-Value Pairs।
प्रश्न 4. copy() क्यों उपयोग करते हैं?
Dictionary की स्वतंत्र Copy बनाने के लिए।
प्रश्न 5. fromkeys() क्या करता है?
दिए गए Keys से नई Dictionary बनाता है।
- get() → Safe Value Access
- keys() → सभी Keys
- values() → सभी Values
- items() → सभी Key-Value Pairs
- update() → Add / Update
- pop() → विशेष Key हटाना
- popitem() → अंतिम Pair हटाना
- setdefault() → Default Key जोड़ना
- copy() → नई Copy
- clear() → Dictionary खाली
- fromkeys() → नई Dictionary
Python Set in Hindi – Set, Frozen Set, Set Operations, Set Methods, Mathematical Operations, Built-in Functions और Practical Programs। इसमें Set से संबंधित सभी Concepts को विस्तार से समझेंगे।