Dictionary Data Type में keys और values की pairs होती है | हर key value के pair को comma(,) से और key और value को colon(:) से seperate किया जाता है | Dictionary के सभी keys और values को curly braces({}) में लिखा जाता है | ये एक immutable data type है |
Creating Dictionary
Dictionary की keys और values को curly braces({}) के अन्दर लिखा जाता है |
dict1 = {"name1":"Navin", "name2":"Gaurav", "name3":"Manvendra"} dict2 = {}
Accessing Value in Dictionary in Python
List और Tuple में value को access करने के लिए उनकी ‘index’ का इस्तेमाल किया जाता है | वैसे ही dictionary की values को access करना हो तो उनकी square brackets([]) में ‘keys’ का इस्तेमाल किया जाता है |
Example: dict = {"name1":"Navin", "name2":"Gaurav", "name3":"Manvendra"} print(dict["name1"]) print(dict["name2"]) print(dict["name3"]) Output: Navin Gaurav Manvendra
अगर invalid key इस्तेमाल की जाती है तो, ‘keyError’ exception आ जाता है |
Example: dict = {"name1":"Navin", "name2":"Gaurav", "name3":"Manvenra"} print(dict["name4"]) Output: print(dict["name4"]) KeyError: 'name4'
Change Dictionary Values in Python
Assignment Operator और square brackets([]) में keys की मदद से dictionary की values को change किया जा सकता है |
Example: dict = {"name1":"Navin", "name2":"Gaurav", "name3":"Manvendra"} dict["name1"] = "Devansh" print(dict["name1"]) print(dict) Output: Devansh {'name1': 'Devansh', 'name2': 'Gaurav', 'name3': 'Manvendra'}
Change Dictionary Keys in Python
pop() method से dictionary की keys को change किया जा सकता है |
pop() method से ‘name1’ key को remove करके दूसरी जगह पर ‘name4’ इस key को add किया गया है
Example: 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 दी जाती है |
Example: dict = {"name1":"Navin", "name2":"Gaurav", "name3":"Manvendra"} print("Before Adding Element :") print(dict) dict["name4"] = "Khushi" print("After Adding Element :") print(dict) Output: Before Adding Element : {'name1': 'Navin', 'name2': 'Gaurav', 'name3': 'Manvendra'} After Adding Element : {'name1': 'Navin', 'name2': 'Gaurav', 'name3': 'Manvendra', 'name4': 'Khushi'}
Delete Dictionary Element in Python
Dictionary में element को delete करने के लिए ‘del’ Operator का इस्तेमाल किया जाता है |
Example: dict = {"name1":"Navin", "name2":"Gaurav", "name3":"Manvendra"} print("Before Deleting Element :") print(dict) del dict["name2"] print("After Deleting Element :") print(dict) Output: Before Deleting Element : {'name1': 'Navin', 'name2': 'Gaurav', 'name3': 'Manvendra'} After Deleting Element : {'name1': 'Navin', 'name3': 'Manvendra'}
Dictionary Functions in Python
Dictionary Function | Description |
---|---|
all() | यदि सभी elements True होते है तो ये तो ये True return करता है | |
any() | एक या सभी 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 करता है | |
Python Programs के लिये यहाँ click करें।