मुख्य कंटेंट पर जाएं
Python Dictionary in Hindi
PYTHON PROGRAMMING 12 मिनट पढ़ें 1 बार पढ़ा गया

Python Dictionary in Hindi

Lavkush Chakrawarti

परिचय

Dictionary क्या है?

Syntax

उदाहरण

Dictionary की विशेषताएँ

Dictionary बनाने के तरीके

1. Direct Dictionary

2. Empty Dictionary

3. dict() Constructor

4. List से Dictionary

Dictionary में विभिन्न Data Types

Dictionary की Length

Dictionary के Elements Access करना

1. Square Bracket द्वारा

2. get() Method द्वारा

Dictionary में नई Key जोड़ना

Value Update करना

Dictionary से Data हटाना

Dictionary Traversing

केवल Keys प्राप्त करना

केवल Values प्राप्त करना

Keys और Values दोनों प्राप्त करना

Membership Operator

Nested Dictionary

Built-in Functions

Dictionary Methods

Practical Programs

Program 1 – Student Information

Program 2 – Marks Update

Program 3 – Dictionary Traversal

Program 4 – Search Key

Program 5 – Nested Dictionary

Common Errors

KeyError

Best Practices

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

Frequently Asked Questions (FAQs)

प्रश्न 1. Dictionary क्या है?

प्रश्न 2. क्या Dictionary में Duplicate Keys हो सकती हैं?

प्रश्न 3. क्या Duplicate Values रख सकते हैं?

प्रश्न 4. Dictionary में Indexing होती है?

प्रश्न 5. get() Method का क्या लाभ है?

Quick Revision

अगला अध्याय

 

परिचय

Programming में कई बार हमें ऐसा Data Store करना होता है जिसमें प्रत्येक Value किसी विशेष पहचान (Key) से जुड़ी होती है। उदाहरण के लिए किसी विद्यार्थी का नाम, रोल नंबर, कक्षा और अंक; किसी कर्मचारी की ID, नाम और वेतन; या किसी Product का नाम, कीमत और Quantity।

यदि इस प्रकार के Data को List या Tuple में रखा जाए, तो किसी विशेष जानकारी को ढूँढना कठिन हो सकता है। ऐसी स्थिति में Dictionary सबसे उपयुक्त Data Type है।

Python में Dictionary एक Built-in Collection Data Type है, जो Key : Value Pair के रूप में Data Store करता है। प्रत्येक Key Unique होती है और उसी Key के माध्यम से संबंधित Value तक पहुँचा जाता है।

Dictionary Mutable होती है, अर्थात् इसमें नए Data जोड़े, बदले और हटाए जा सकते हैं।

 

Dictionary क्या है?

Dictionary Python का एक Built-in Collection Data Type है जिसमें Data को Key : Value Pair के रूप में Curly Braces { } के अंदर लिखा जाता है।

Syntax

dictionary_name = {

    "key1": value1,

    "key2": value2

}

 

उदाहरण

student = {

    "name": "Rahul",

    "age": 20,

    "course": "BCA"

}

 

Dictionary की विशेषताएँ

  • Data Key : Value Pair में Store होता है।
  • प्रत्येक Key Unique होती है।
  • Values Duplicate हो सकती हैं।
  • Dictionary Mutable होती है।
  • Dictionary Ordered होती है (Python 3.7+ से)।
  • Indexing उपलब्ध नहीं होती।
  • Key के माध्यम से Data Access किया जाता है।
  • Nested Dictionary बनाई जा सकती है।

 

Dictionary बनाने के तरीके

  1. Direct Dictionary

student = {

    "name": "Rahul",

    "age": 20

}

 

  1. Empty Dictionary

student = {}

 

  1. dict() Constructor

student = dict(

    name="Rahul",

    age=20,

    city="Lucknow"

)

 

  1. List से Dictionary

student = dict([

    ("name", "Rahul"),

    ("age", 20)

])

 

Dictionary में विभिन्न Data Types

data = {

    "name": "Rahul",

    "age": 20,

    "marks": 85.5,

    "passed": True,

    "subjects": ["Python", "C", "Java"]

}

 

Dictionary की Length

student = {

    "name":"Rahul",

    "age":20,

    "city":"Lucknow"

}

 

print(len(student))

Output

3

 

Dictionary के Elements Access करना

  1. Square Bracket द्वारा

student = {

    "name":"Rahul",

    "age":20

}

 

print(student["name"])

Output

Rahul

 

  1. get() Method द्वारा

print(student.get("age"))

Output

20

यदि Key मौजूद नहीं है, तो get() Error नहीं देता बल्कि None (या Default Value) लौटाता है।

 

Dictionary में नई Key जोड़ना

student = {

    "name":"Rahul"

}

 

student["city"] = "Lucknow"

 

print(student)

 

Value Update करना

student = {

    "age":20

}

 

student["age"] = 21

 

print(student)

 

Dictionary से Data हटाना

student = {

    "name":"Rahul",

    "age":20

}

 

del student["age"]

 

print(student)

 

Dictionary Traversing

student = {

    "name":"Rahul",

    "age":20,

    "city":"Lucknow"

}

 

for key in student:

    print(key, student[key])

 

केवल Keys प्राप्त करना

for key in student.keys():

    print(key)

 

केवल Values प्राप्त करना

for value in student.values():

    print(value)

 

Keys और Values दोनों प्राप्त करना

for key, value in student.items():

    print(key, value)

 

Membership Operator

student = {

    "name":"Rahul",

    "age":20

}

 

print("name" in student)

print("city" not in student)

 

Nested Dictionary

students = {

    "student1":{

        "name":"Rahul",

        "marks":90

    },

    "student2":{

        "name":"Neha",

        "marks":95

    }

}

Access

print(students["student1"]["name"])

Output

Rahul

 

Built-in Functions

Function

उपयोग

len()

कुल Key-Value Pair

type()

Data Type

sorted()

Keys को Sort करना

max()

सबसे बड़ी Key

min()

सबसे छोटी Key

 

Dictionary Methods

Python Dictionary के महत्वपूर्ण Methods—

  • clear()
  • copy()
  • fromkeys()
  • get()
  • items()
  • keys()
  • values()
  • pop()
  • popitem()
  • setdefault()
  • update()

अगले अध्याय में इन सभी Methods को एक ही Master Guide में विस्तार से पढ़ेंगे।

 

Practical Programs

Program 1 – Student Information

student = {

    "name":"Rahul",

    "age":20,

    "marks":85

}

 

print(student)

 

Program 2 – Marks Update

student = {

    "marks":80

}

 

student["marks"]=90

 

print(student)

 

Program 3 – Dictionary Traversal

student={

    "name":"Rahul",

    "city":"Lucknow"

}

 

for key,value in student.items():

    print(key,value)

 

Program 4 – Search Key

student={

    "name":"Rahul",

    "age":20

}

 

if "age" in student:

    print("Key Found")

 

Program 5 – Nested Dictionary

students={

    "A":{

        "marks":80

    },

    "B":{

        "marks":90

    }

}

 

print(students["B"]["marks"])

 

Common Errors

KeyError

student={

    "name":"Rahul"

}

 

print(student["city"])

❌ क्योंकि "city" Key मौजूद नहीं है।

 

Best Practices

  • हमेशा Unique Keys का उपयोग करें।
  • Unknown Key के लिए get() Method का उपयोग करें।
  • Related Data को Dictionary में Store करें।
  • Nested Data के लिए Nested Dictionary का उपयोग करें।
  • यदि Data का क्रम महत्वपूर्ण हो, तो Python 3.7+ का उपयोग करें।

 

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

  • Dictionary Key : Value Pair में Data Store करती है।
  • Dictionary Mutable होती है।
  • Keys Unique होती हैं।
  • Values Duplicate हो सकती हैं।
  • Dictionary में Indexing नहीं होती।
  • get() KeyError से बचाता है।
  • items() Key और Value दोनों Return करता है।

 

Frequently Asked Questions (FAQs)

प्रश्न 1. Dictionary क्या है?

Dictionary Python का Mutable Collection Data Type है जो Data को Key : Value Pair में Store करता है।

प्रश्न 2. क्या Dictionary में Duplicate Keys हो सकती हैं?

नहीं, प्रत्येक Key Unique होती है।

प्रश्न 3. क्या Duplicate Values रख सकते हैं?

हाँ।

प्रश्न 4. Dictionary में Indexing होती है?

नहीं, Data को Key के माध्यम से Access किया जाता है।

प्रश्न 5. get() Method का क्या लाभ है?

यदि Key मौजूद नहीं हो, तो KeyError नहीं आता।

 

Quick Revision

  • Dictionary = Key : Value Pair
  • Curly Braces { }
  • Mutable = ✔
  • Keys = Unique
  • Values = Duplicate Allowed
  • Indexing = ✘
  • Access = Key द्वारा
  • Nested Dictionary = ✔
  • get() = Safe Access
  • items() = Keys + Values

 

अगला अध्याय

Python Dictionary Methods in Hindi – get(), keys(), values(), items(), update(), pop(), popitem(), setdefault(), copy(), clear(), fromkeys() आदि सभी Dictionary Methods की Complete Guide। इसमें सभी Methods को एक ही Article में विस्तार से समझाया जाएगा।

 

निष्कर्ष (Conclusion)

Python Dictionary की पूरी जानकारी हिंदी में। Dictionary क्या है, Keys, Values, Creating Dictionary, Access, Update, Nested Dictionary, Dictionary Methods, Built-in Functions, 40+ Examples और Practical Programs सीखें।