Dictionary
- Dictionary Python का एक Mutable , Ordered और Key-Value Pair आधारित Data Type है।
- इसका उपयोग डेटा को Key और Value के रूप में Store करने के लिए किया जाता है।
- Dictionary में Data Curly Braces
{}के अंदर लिखा जाता है। - प्रत्येक Item Key : Value के रूप में होता है।
- Keys Unique (अद्वितीय) होती हैं, जबकि Values Duplicate हो सकती हैं।
- Python 3.7 और उसके बाद के Version में Dictionary Ordered होती है।
Syntax :
dictionary_name = {
key1: value1,
key2: value2,
key3: value3
}
Characteristics of Dictionary
| Characteristics | Description |
|---|---|
| Ordered | Items का क्रम बना रहता है (Python 3.7+) |
| Mutable | Dictionary में Data को बदला जा सकता है। |
| Key-Value Pair | प्रत्येक Item एक Key और उसकी Value से बना होता है। |
| Unique Keys | प्रत्येक Key अलग (Unique) होनी चाहिए। |
| Duplicate Values | एक ही Value कई Keys के साथ हो सकती है। |
| Mixed Data Types | Key और Value में अलग-अलग Data Types हो सकते हैं। |
| Dynamic Size | आवश्यकता अनुसार नए Items जोड़ या हटाए जा सकते हैं। |
Creating Dictionary
Dictionary की keys और values को curly brackets ({}) के अंदर लिखा जाता है |
Source Code :
dict1 = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
dict2 = {}
Accessing value in Dictionary in Python
List और tuple में value को access करने के लिए उनकी ‘index’ का use किया जाता है वैसे ही dictionary की value को access करना हो तो उनकी square brackets ([]) में ‘keys’ का use किया जाता है |
Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
print(dict["name1"])
print(dict["name2"])
print(dict["name3"])
अगर invalid key इस्तेमाल की जाती है तो ‘key error ‘ exception आ जाता है |
Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
print(dict["name4"])
Change dictionary values in Python
Assignment operator और square brackets में keys की मदद से dictionary की value को change किया जा सकता है |
Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamalesh"}
dict["name1"] = "Nagesh"
print(dict["name1"])
print(dict)
Change Dictionary keys in Python
pop() method से dictionary की keys को change किया जा सकता है | pop() method से ‘name1’ keys को remove करके दूसरी जगह पर ‘name4’ पर इस keys को 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)
Add dictionary element in python
Dictionary में element add करने के लिए square brackets ([]) में 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)
Delete Dictionary element in Python
Dictionary में element को delete करने के लिए ‘del’ operator का use किया जाता है |
Source Code :
dict = {"name1":"Rakesh", "name2":"Ramesh", "name3":"Kamlesh"}
print("Before Deleting Element :")
print(dict)
del dict["name2"]
print("After Deleting Element :")
print(dict)
Dictionary Function in Python
| Function | Description | Syntax | Example | Output |
|---|---|---|---|---|
len() | Dictionary में कुल Key-Value Pairs की संख्या बताता है। | len(dict) | len({"a":1,"b":2}) | 2 |
type() | Variable का Data Type बताता है। | type(dict) | type({"a":1}) | <class 'dict'> |
dict() | नया Dictionary बनाता है या Iterable को Dictionary में Convert करता है। | dict() | dict(name="Rahul", age=21) | {'name':'Rahul','age':21} |
str() | Dictionary को String में Convert करता है। | str(dict) | str({"a":1}) | "{'a': 1}" |
sorted() | Dictionary की Keys को Sort करके List Return करता है। | sorted(dict) | sorted({"b":2,"a":1}) | ['a', 'b'] |
max() | सबसे बड़ी Key Return करता है। | max(dict) | max({"a":1,"b":2}) | 'b' |
min() | सबसे छोटी Key Return करता है। | min(dict) | min({"a":1,"b":2}) | 'a' |
sum() | यदि Keys Numeric हों, तो उनका योग Return करता है। | sum(dict) | sum({1:"A",2:"B"}) | 3 |
any() | यदि कम से कम एक Key True है, तो True Return करता है। | any(dict) | any({0:"A",1:"B"}) | True |
all() | यदि सभी Keys True हैं, तो True Return करता है। | all(dict) | all({1:"A",2:"B"}) | True |
list() | Dictionary की सभी Keys को List में Convert करता है। | list(dict) | list({"a":1,"b":2}) | ['a', 'b'] |
tuple() | Dictionary की सभी Keys को Tuple में Convert करता है। | tuple(dict) | tuple({"a":1,"b":2}) | ('a', 'b') |
