Python in Hindi - Dictionary


Python आपको एक और Lists जैसा collection provide करती है जिसे dictionary कहा जाता है। Lists की ही तरह एक dictionary को आसानी से change किया जा सकता है और आवश्यकतानुसार घटाया और बढ़ाया जा सकता है।
List और dictionary में फर्क यह होता है की list में elements ordered होते है, लेकिन एक dictionary में elements ordered नहीं होते है।
इसके अलावा dictionary collection indexing को support नहीं करता है। इसका मतलब यह हुआ की dictionary में किसी element को आप उसकी index या position के द्वारा access नहीं कर सकते है। Dictionary में elements को keys के द्वारा access किया जाता है।
Dictionary में कोई भी element key और value के pair में store किया जाता है। हर key एक value को point करती है या फिर उसे store और access करने के लिए use की जाती है।
एक dictionary के अंतर्गत किसी key की value python के किसी भी type की हो सकती है। यानी की strings, lists, tuples और dictionary को आप key की value के रूप में use कर सकते है।
लेकिन dictionary में आप हर type की key नहीं define कर सकते है। Dictionary में key define करने के लिए आप सिर्फ immutable (ऐसे types जो unchangeable है या जिनकी values को change नहीं किया जा सकता है) types को ही use कर सकते है। यानी tuples, strings, integers, floats आदि को key के रूप में use किया जा सकता है।
आप dictionary में value के रूप में किसी list को store कर सकते है और एक list में भी value के रूप में dictionary को store किया जा सकता है।
Dictionary collection list, tuples आदि के साथ perform होने वाले operations को support नहीं करता है। आप dictionary के साथ concatenation आदि operations perform नहीं कर सकते है। Dictionary का मुख्य उद्देश्य mapping होता है। यानि की एक key को एक value से map करना।

Creating Python Dictionaries

एक Dictionary के items को curly brackets में define किया जाता है। Dictionary में items को key:value के pair में लिखा जाता है। Key और value को colon से separate किया जाता है। Colon के left side में key होती है और right side में value होती है। Key:value के हर pair को comma से separate किया जाता है। 
उदाहरण के लिए निचे दिए गए code को देखिये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
print(myDictionary)
ऊपर दिए गए उदाहरण में myDict के नाम से एक dictionary collection create किया गया है। इस dictionary में c1, c2 और c3 keys है और Red, Green और Blue values है। एक बात आपको ध्यान रखनी चाहिए की string keys और string values को हमेशा double quotes में ही लिखा जाना चाहिए। 
Tuples को आप keys के रुप में use कर सकते है। उदाहरण के लिए निचे दिए गए code को देखिये।
myDict = {(Apple, Chilli, Tomato):”Red”,(Calabash, Cabbage,Beans):”Green”}
ऊपर दिए गए उदाहरण में tuples को keys के रूप में use किया गया है।
यदि आप एक existing dictionary में कोई नया item add करना चाहते है तो ऐसा आप एक नया नाम और नयी value define करके करते है। उदाहरण के लिए निचे दिए गए code को देखिये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
print(myDict)

myDict[“c4”] = “Lime”
print(myDict)
ऊपर दिए गए उदाहरण में myDict dictionary में c4 key और Lime value बाद में separately add की गयी है।
आप dict() constructor द्वारा भी dictionary create कर सकते है। जब आप dict() constructor के प्रयोग से dictionary create करते है तो keys को double quotes में नहीं लिखते है और colon (:) की जगह assignment operator (=) का प्रयोग करते है। इसका उदाहरण निचे दिया जा रहा है।
myDict = dict(c1=”Red”,c2=”Green”,c3=”Blue”)
print(myDict)
ऊपर दिए गए उदाहरण में dict() constructor द्वारा dictionary collection create किया गया है।

Accessing Python Dictionaries

किसी dictionary से items access करने के लिए उन्हें keys के द्वारा refer किया जाता है। उदाहरण के लिए निचे दिए गए code को देखिये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
print(myDict[“c1”])
ऊपर दिए गए उदाहरण में c1 key द्वारा Red value को print किया गया है।
किसी dictionary में से value access करने के लिए आप get() method का भी प्रयोग कर सकते है। यह method argument के रूप में key को लेता है। उदाहरण के लिए निचे दिए गए code को देखिये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
print(myDict.get(“c2”))
ऊपर दिए गए उदाहरण में get() method द्वारा c2 key की value Green को print किया गया है। 
आप किसी dictionary के सभी elements को print करने के लिए उसे iterate कर सकते है। इसके लिए for loop और in operator का प्रयोग किया जाता है। उदाहरण के लिए निचे दिए गए code को देखिये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}

for key in myDict
print(myDict[key])
ऊपर दिए गए उदाहरण में for loop द्वारा myDict dictionary के सभी elements को print किया गया है। जब आप इस प्रकार किसी dictionary को iterate करते है तो dictionary की keys return की जाती है।
इसके अलावा आप किसी dictionary की keys को iterate करने के लिए iterkeys() method और values को iterate करने के लिए itervalues() method use कर सकते है। उदाहरण के लिये निचे दिए गए code को देखिये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}

for key in MyDict.iterkeys()
print(myDict[key])
for value in myDict.itervalues()
print(myDict[value])
ऊपर दिए गए उदाहरण में myDict dictionary की keys और values को for loop के साथ iterkeys() और itervalues() methods को प्रयोग करके print किया गया है।
इन दोनों functions के अलावा आप items() function को भी use कर सकते है। यह function keys और values को एक साथ return करता है।

Updating Python Dictionaries

Dictionaries में यदि आप किसी member की value को change करना चाहते है तो ऐसा आप उसकी key के द्वारा कर सकते है। आप key के द्वारा उस member को नयी value assign करवा सकते है।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}

print(myDict)
myDict[“c1”] = “Lime”
print(myDict)
ऊपर दिए गए उदाहरण में c1 key की value को Red से Lime में change किया गया है।

Deleting Python Dictionaries 

Dictionaries में से items को delete करने के लिए आपको tuples की तरह उन्हें combine करने की आवश्यकता नहीं होती है। Del keyword द्वारा आप किसी specific key वाले item को remove कर सकते है। उदाहरण के लिए निचे दिया गया code देखिये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
print(myDict)

del myDict[“c2”]
print(myDict)
ऊपर दिए गए उदाहरण में del command द्वारा second item को remove किया गया है।
इसके अलावा आप pop() method द्वारा भी किसी item को remove कर सकते है। इस method में key को argument के रूप में pass किया जाता है। इसका उदाहरण निचे दिया जा रहा है।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
print(myDict)

myDict.pop(“c3”)
print(myDict)
ऊपर दिए गए उदाहरण में pop() method द्वारा myDict dictionary के तीसरे item को remove किया गया है।
इसके अलावा dictionary के last item को remove करने के लिए आप popitem() method को use कर सकते है।
Del keyword द्वारा आप एक सम्पूर्ण dictionary को भी delete कर सकते है। इसके अलावा यदि आप एक dictionary को खाली करना चाहते है तो इसके लिए clear() method use कर सकते है। इस method से dictionary के सभी elements delete हो जाते है।

Methods for Python Dictionaries

Python में dictionaries के लिए निचे दिए गए methods available है।
  • copy()
  • sort()
  • len()
  • cmp()
  • update()
  • str()
  • items()
इन methods के अलावा आप dictionaries के साथ in और not in जैसे operators भी use कर सकते है।

Post a Comment

0 Comments