Python Dictionary In Hindi

📔 : Python 🔗

Python में dictionary उन 4 built in data types (list , tuple , set , dictionary) में से एक है जिसका use data collections को store करने किया जाता है या कह सकते हैं multiple values store करने के लिए किया जाता है।


Python में dictionary का use key : value type का data store करने के लिए किया जाता है। मतलब dictionary में से कोई particular value / item निकालने के लिए key का use करेंगे।


जबकि list , tuple , set में indexed data stored होता था। जिसे हम item index या for loop के through access करते थे।

Python में dictionary ordered , changeable है और unique key ही allow करता है।

  1. Ordered - ordered मतलब जिस order में हमने items को dictionary में insert किया था हमें उसी order में मिलेंगे।

  2. unique key - means हम same key नहीं रख सकते हैं , value तो same हो सकती है लेकिन key नहीं।

  3. Changeable - मतलब उन items को जरूरत पड़ने पर update कर सकते हैं या dictionary में नए items को add कर सकते हैं।


Python version 3.6 तक तो dictionary unordered थी , लेकिन version 3.7 में update किया गया और अब ये ordered ही हैं।


dictionary को curly brackets { } किया जाता है जिसमे key : value pair में data store करते हैं। और एक से ज्यादा items होने पर उन्हें comma , से separate करते हैं।

Python dictionary Syntax

d = {
  'key1' : 'value1',
  'key2' : 'value2',
  .
  .
  .
  'key_n' : 'value_n',
}

Python dictionary Example

Copy Fullscreen Close Fullscreen Run
d = {
    'name' : 'Aryendra Sharma',
    'age' : 25,
    'city' : 'Agra',
    'country' : 'India'
}

print(d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_ex.py
{'name': 'Aryendra Sharma', 'age': 25, 'city': 'Agra', 'country': 'India'}

Python dictionary type() and len()

Dictionary की length जानने के लिए len() function का use किया जाता है और , type जानने के लिए type() function का use किया जाता है।
See Example -

Copy Fullscreen Close Fullscreen Run
d = {
    'name' : 'Aryendra Sharma',
    'age' : 25,
    'city' : 'Agra',
    'country' : 'India'
}

print('Type :', type(d))
print('Length :', len(d))
Output
C:\Users\Rahulkumar\Desktop\python>python dict_type_len.py
Type : <class 'dict'="">
Length : 4

Remember

Dictionary की keys बनाते समय कुछ बातों का जरूर ध्यान रखें -

  1. keys को आप String (जैसे : 'name', 'age') , Boolean (जैसे : True , False) , numeric (जैसे : 0 , 1 , 1.5) type का ले सकते है।

  2. even empty String (जैसे : '', ' ', '  ', '   ')  भी ले सकते हैं। यहां empty string with single space , double space अलग - अलग count होंगी।

  3. लेकिन आप set (जैसे : {3}) , list (जैसे : [4]), tuple (जैसे : (5) ) को as a key नहीं ले सकते हैं , हाँ as a value तो ले सकते हैं उसमे कोई problem नहीं है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers