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

Python Dictionary Methods in Hindi

Lavkush Chakrawarti

परिचय

Dictionary Methods क्या हैं?

Syntax

Python Dictionary Methods की सूची

1. get()

Syntax

उदाहरण

2. keys()

उदाहरण

3. values()

4. items()

5. update()

उदाहरण

6. pop()

Syntax

उदाहरण

7. popitem()

8. setdefault()

9. copy()

copy() और Assignment में अंतर

10. clear()

11. fromkeys()

सभी Methods का सारांश

Practical Programs

Program 1 – Student Record

Program 2 – Safe Search

Program 3 – Dictionary Traversal

Program 4 – Update Record

Program 5 – Remove Key

Common Errors

KeyError

Best Practices

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

Frequently Asked Questions (FAQs)

प्रश्न 1. get() और Square Bracket ([]) में क्या अंतर है?

प्रश्न 2. update() का उपयोग क्यों किया जाता है?

प्रश्न 3. items() क्या Return करता है?

प्रश्न 4. copy() क्यों उपयोग करते हैं?

प्रश्न 5. fromkeys() क्या करता है?

Quick Revision

अगला अध्याय

 

परिचय

Python में Dictionary एक शक्तिशाली Collection Data Type है, जिसमें Data को Key : Value Pair के रूप में रखा जाता है। Dictionary बनने के बाद उसमें नई Entries जोड़ना, पुरानी Values बदलना, किसी Key की Value प्राप्त करना, Data हटाना, पूरी Dictionary साफ़ करना या उसकी Copy बनाना जैसे कार्य बार-बार करने पड़ते हैं।

इन सभी कार्यों के लिए Python कई Built-in Dictionary Methods प्रदान करता है।

इस अध्याय में Dictionary के सभी महत्वपूर्ण Methods को Syntax, Examples, Output, Practical Programs और परीक्षा की दृष्टि से महत्वपूर्ण तथ्यों सहित विस्तार से समझेंगे।

 

Dictionary Methods क्या हैं?

Dictionary Methods वे Built-in Functions हैं जो केवल Dictionary Object पर कार्य करते हैं।

Syntax

dictionary.method_name(arguments)

उदाहरण

student = {

    "name": "Rahul"

}

 

print(student.get("name"))

Output

Rahul

 

Python Dictionary Methods की सूची

क्रम

Method

उपयोग

1

get()

Key की Value प्राप्त करना

2

keys()

सभी Keys प्राप्त करना

3

values()

सभी Values प्राप्त करना

4

items()

सभी Key-Value Pair प्राप्त करना

5

update()

Dictionary Update करना

6

pop()

किसी Key को हटाना

7

popitem()

अंतिम Key-Value Pair हटाना

8

setdefault()

Default Value Set करना

9

copy()

Dictionary की Copy बनाना

10

clear()

पूरी Dictionary खाली करना

11

fromkeys()

Keys से नई Dictionary बनाना

 

  1. get()

किसी Key की Value प्राप्त करने के लिए उपयोग किया जाता है।

Syntax

dictionary.get(key, default_value)

उदाहरण

student = {

    "name": "Rahul",

    "age": 20

}

 

print(student.get("name"))

Output

Rahul

यदि Key मौजूद नहीं हो:

print(student.get("city"))

Output

None

Default Value के साथ:

print(student.get("city", "Not Available"))

Output

Not Available

 

  1. keys()

Dictionary की सभी Keys प्राप्त करने के लिए।

उदाहरण

student = {

    "name":"Rahul",

    "age":20

}

 

print(student.keys())

Output

dict_keys(['name', 'age'])

 

  1. values()

Dictionary की सभी Values प्राप्त करने के लिए।

print(student.values())

Output

dict_values(['Rahul', 20])

 

  1. items()

सभी Key-Value Pair प्राप्त करने के लिए।

print(student.items())

Output

dict_items([('name', 'Rahul'), ('age', 20)])

Loop में उपयोग:

for key, value in student.items():

    print(key, value)

 

  1. update()

नई Key जोड़ने या पुरानी Value बदलने के लिए।

उदाहरण

student = {

    "name":"Rahul"

}

 

student.update({"city":"Lucknow"})

 

print(student)

Output

{'name':'Rahul','city':'Lucknow'}

Value Update:

student.update({"name":"Amit"})

 

  1. pop()

किसी विशेष Key को हटाने के लिए।

Syntax

dictionary.pop(key)

उदाहरण

student = {

    "name":"Rahul",

    "age":20

}

 

student.pop("age")

 

print(student)

Output

{'name':'Rahul'}

 

  1. popitem()

Dictionary का अंतिम Key-Value Pair हटाता है।

student = {

    "name":"Rahul",

    "age":20

}

 

student.popitem()

 

print(student)

 

  1. setdefault()

यदि Key मौजूद हो तो उसकी Value लौटाता है, अन्यथा नई Key जोड़ देता है।

student = {

    "name":"Rahul"

}

 

student.setdefault("city","Lucknow")

 

print(student)

Output

{'name':'Rahul','city':'Lucknow'}

 

  1. copy()

Dictionary की अलग Copy बनाता है।

student = {

    "name":"Rahul"

}

 

new_student = student.copy()

 

print(new_student)

 

copy() और Assignment में अंतर

a = {"x":10}

 

b = a

 

b["x"] = 100

 

print(a)

Output

{'x':100}

 

a = {"x":10}

 

b = a.copy()

 

b["x"] = 100

 

print(a)

print(b)

Output

{'x':10}

{'x':100}

 

  1. clear()

पूरी Dictionary खाली कर देता है।

student = {

    "name":"Rahul",

    "age":20

}

 

student.clear()

 

print(student)

Output

{}

 

  1. fromkeys()

Keys की सहायता से नई Dictionary बनाता है।

keys = ("name","age","city")

 

student = dict.fromkeys(keys)

 

print(student)

Output

{'name':None,'age':None,'city':None}

Default Value के साथ

student = dict.fromkeys(keys,0)

 

print(student)

Output

{'name':0,'age':0,'city':0}

 

सभी Methods का सारांश

Method

Return Value

Original Dictionary बदलती है?

get()

Value

keys()

View Object

values()

View Object

items()

View Object

update()

None

pop()

Value

popitem()

Key-Value Pair

setdefault()

Value

✔ (यदि Key न हो)

copy()

नई Dictionary

clear()

None

fromkeys()

नई Dictionary

 

Practical Programs

Program 1 – Student Record

student = {}

 

student["name"] = "Rahul"

student["marks"] = 95

 

print(student)

 

Program 2 – Safe Search

student = {

    "name":"Rahul"

}

 

print(student.get("city","City Not Found"))

 

Program 3 – Dictionary Traversal

student = {

    "name":"Rahul",

    "age":20

}

 

for key,value in student.items():

    print(key,value)

 

Program 4 – Update Record

student = {

    "name":"Rahul"

}

 

student.update({"age":20})

 

print(student)

 

Program 5 – Remove Key

student = {

    "name":"Rahul",

    "city":"Lucknow"

}

 

student.pop("city")

 

print(student)

 

Common Errors

KeyError

student = {

    "name":"Rahul"

}

 

student.pop("city")

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

 

Best Practices

  • Unknown Key के लिए get() का उपयोग करें।
  • Loop में items() सबसे उपयोगी Method है।
  • Dictionary Merge करने के लिए update() उपयोग करें।
  • Original Data सुरक्षित रखना हो तो copy() का उपयोग करें।
  • Temporary Data हटाने के लिए pop() उपयोग करें।

 

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

  • get() KeyError से बचाता है।
  • keys() सभी Keys Return करता है।
  • values() सभी Values Return करता है।
  • items() Key और Value दोनों Return करता है।
  • update() नई Entries जोड़ता या पुरानी बदलता है।
  • pop() विशेष Key हटाता है।
  • popitem() अंतिम Key-Value Pair हटाता है।
  • copy() नई Dictionary बनाता है।
  • clear() पूरी Dictionary खाली कर देता है।
  • fromkeys() नई Dictionary बनाने के लिए उपयोग होता है।

 

Frequently Asked Questions (FAQs)

प्रश्न 1. get() और Square Bracket ([]) में क्या अंतर है?

get() Key न मिलने पर Error नहीं देता, जबकि [] से KeyError आता है।

प्रश्न 2. update() का उपयोग क्यों किया जाता है?

नई Key जोड़ने और पुरानी Value बदलने के लिए।

प्रश्न 3. items() क्या Return करता है?

सभी Key-Value Pairs।

प्रश्न 4. copy() क्यों उपयोग करते हैं?

Dictionary की स्वतंत्र Copy बनाने के लिए।

प्रश्न 5. fromkeys() क्या करता है?

दिए गए Keys से नई Dictionary बनाता है।

 

Quick Revision

  • get() → Safe Value Access
  • keys() → सभी Keys
  • values() → सभी Values
  • items() → सभी Key-Value Pairs
  • update() → Add / Update
  • pop() → विशेष Key हटाना
  • popitem() → अंतिम Pair हटाना
  • setdefault() → Default Key जोड़ना
  • copy() → नई Copy
  • clear() → Dictionary खाली
  • fromkeys() → नई Dictionary

 

अगला अध्याय

Python Set in Hindi – Set, Frozen Set, Set Operations, Set Methods, Mathematical Operations, Built-in Functions और Practical Programs। इसमें Set से संबंधित सभी Concepts को विस्तार से समझेंगे।

 

निष्कर्ष (Conclusion)

Python Dictionary Methods की पूरी जानकारी हिंदी में। get(), keys(), values(), items(), update(), pop(), popitem(), setdefault(), copy(), clear(), fromkeys() सहित सभी Dictionary Methods उदाहरण और Practical Programs के साथ सीखें।