Dictionary; hashes की तरह होती है | Dictionary में key और value की pairs होती है |
Dictionary में key और value को colon(:) से seperate किया जाता है और key और value की pair को comma से seperate किया जाता है |
Dictionary; mutable(changeable) होता है लेकिन Dictionary में values change हो सकती है और method(pop()) के जरिये keys भी change की जा सकती है लेकिन duplicate key नहीं दी जा सकती |
Creating Dictionary
Dictionary की keys और values को curly braces({}) के अन्दर लिखा जाता है |dict1 = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
dict2 = {}
Accessing Value in Dictionary in Python
List और Tuple में value को access करने के लिए उनकी 'index' का इस्तेमाल किया जाता है | वैसे ही dictionary की values को access करना हो तो उनकी square brackets([]) में 'keys' का इस्तेमाल किया जाता है |Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
print(dict["name1"])
print(dict["name2"])
print(dict["name3"])
Output :
Rakesh Ramesh Kamalesh
अगर invalid key इस्तेमाल की जाती है तो, 'keyError' exception आ जाता है |
Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
print(dict["name4"])
Output :
print(dict["name4"]) KeyError: 'name4'
Change Dictionary Values in Python
Assignment Operator और square brackets([]) में keys की मदद से dictionary की values को change किया जा सकता है |Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
dict["name1"] = "Nagesh"
print(dict["name1"])
print(dict)
Output :
Nagesh
{'name1': 'Nagesh', 'name2': 'Ramesh', 'name3': 'Kamalesh'}
Change Dictionary Keys in Python
pop() method से dictionary की keys को change किया जा सकता है |pop() method से 'name1' key को remove करके दूसरी जगह पर 'name4' इस key को add किया गया है |
Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
print("Before Changing Key :")
print(dict)
dict["name4"] = dict.pop("name1")
print("After Changing Key :")
print(dict)
Output :
Before Changing Key :
{'name1': 'Rakesh', 'name2': 'Ramesh', 'name3': 'Kamalesh'}
After Changing Key :
{'name2': 'Ramesh', 'name3': 'Kamalesh', 'name4': 'Rakesh'}
Add Dictionary Element in Python
Dictionary में element add करने के लिए square bracket([]) में unique key name और उसकी value दी जाती है |Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamlesh"}
print("Before Adding Element :")
print(dict)
dict["name4"] = "Rajesh"
print("After Adding Element :")
print(dict)
Output :
Before Adding Element :
{'name1': 'Rakesh', 'name2': 'Ramesh', 'name3': 'Kamlesh'}
After Adding Element :
{'name1': 'Rakesh', 'name2': 'Ramesh', 'name3': 'Kamlesh', 'name4': 'Rajesh'}
Delete Dictionary Element in Python
Dictionary में element को delete करने के लिए 'del' Operator का इस्तेमाल किया जाता है |Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamlesh"}
print("Before Deleting Element :")
print(dict)
del dict["name2"]
print("After Deleting Element :")
print(dict)
Output :
Before Deleting Element :
{'name1': 'Rakesh', 'name2': 'Ramesh', 'name3': 'Kamlesh'}
After Deleting Element :
{'name1': 'Rakesh', 'name3': 'Kamlesh'}
Dictionary Functions in Python
| Dictionary Function | Description |
|---|---|
| all() | sequence के सभी elements True होते है तो ये तो ये True return करता है | |
| any() | sequence का एक या सभी elements True होते है तो ये तो ये True return करता है | |
| dict() | इसका इस्तेम्मल नए ढंग से dictionary को create करने के लिए किया जाता है | |
| len() | dictionary की length को return करता है | |
| sorted() | दिए गए sequence को sort करके return करता है | |
| str() | dictionary को string में convert करके return करता है | |
Dictionary Methods in Python
| Dictionary Method | Description |
|---|---|
| clear() | dictionary को clear करता है | |
| copy() | Dictionary को copy करके return करता है | |
| fromkeys() | दिए हुए sequence के item को dictionary के key के रूप में return करता है | |
| get() | दिए गए key की value को return करता है | |
| has_key() | दी हुई key; dictionary पर है या नहीं ये boolean value में return करता है | |
| items() | (key, value) इस नए प्रकार से dictionary को return करता है | |
| keys() | dictionary के सिर्फ keys को return करता है | |
| pop() | दिए गए key को remove करके उसकी value return की जाती है | अगर दी हुई key नहीं मिल जाती तो set की हुई default value return की जाती है | |
| popitem() | दिए गए dictionary के last item को remove करके (key, value) return करता है | |
| setdefault() | दी हुई key exist होती है तो उसकी value return की जाती है | अगर key नहीं होती है तो set की हुई default value को return किया जाता है | |
| update() | old dictionary को update करके update हुई dictionary को return करता है | |
| values() | dictionary के सिर्फ keys की values को return करता है | |







No comments: