Python Dictionary क्या है? Keys, Values & Methods in Hindi | get(), update(), pop() | RBSE Class 12

इस लेख में: Python Dictionary क्या है, key-value pair, Dictionary creation, data access, add, update, delete तथा keys(), values(), items(), get(), update(), pop(), popitem() और clear() जैसे important methods को आसान Hindi में examples के साथ समझेंगे।

Python Dictionary क्या है?

Dictionary Python का एक built-in data structure है, जिसका उपयोग data को key-value pairs के रूप में store करने के लिए किया जाता है।

Dictionary में प्रत्येक key के साथ एक corresponding value होती है। इससे किसी value को उसकी key के माध्यम से access किया जा सकता है।

Example

student = {
    "name": "Rahul",
    "age": 18,
    "class": 12
}

print(student)

Output:

{'name': 'Rahul', 'age': 18, 'class': 12}
Important: Dictionary में data को सामान्यतः key : value pair के रूप में लिखा जाता है।

Dictionary का Syntax

dictionary_name = {
    key1: value1,
    key2: value2,
    key3: value3
}

Python Dictionary की मुख्य विशेषताएँ

  • Dictionary data को key-value pairs में store करती है।
  • Dictionary mutable होती है।
  • Keys unique होनी चाहिए।
  • Values duplicate हो सकती हैं।
  • Dictionary में values को keys की सहायता से access किया जाता है।
  • Dictionary में data add, update और delete किया जा सकता है।

Empty Dictionary

Empty Dictionary बनाने के लिए {} का उपयोग किया जा सकता है।

student = {}

print(student)
print(type(student))

Output:

{}
<class 'dict'>

Dictionary में Data Access करना

Dictionary की value को उसकी key का उपयोग करके access किया जा सकता है।

student = {
    "name": "Rahul",
    "age": 18,
    "class": 12
}

print(student["name"])
print(student["age"])

Output:

Rahul
18

get() Method

get() method की सहायता से Dictionary की value को key के आधार पर प्राप्त किया जा सकता है।

student = {
    "name": "Rahul",
    "age": 18
}

print(student.get("name"))

Output:

Rahul

get() और [] में अंतर

यदि specified key मौजूद नहीं है, तो get() सामान्यतः None return करता है जबकि square bracket से access करने पर KeyError हो सकता है।

student = {
    "name": "Rahul"
}

print(student.get("city"))

Output:

None

Dictionary में नया Item Add करना

नई key-value pair को assignment के द्वारा Dictionary में add किया जा सकता है।

student = {
    "name": "Rahul",
    "age": 18
}

student["city"] = "Jaipur"

print(student)

Output:

{'name': 'Rahul', 'age': 18, 'city': 'Jaipur'}

Dictionary में Value Update करना

यदि key पहले से मौजूद है और उसी key को नई value assign की जाती है, तो existing value update हो जाती है।

student = {
    "name": "Rahul",
    "age": 18
}

student["age"] = 19

print(student)

Output:

{'name': 'Rahul', 'age': 19}

update() Method

update() method की सहायता से Dictionary में एक या अधिक key-value pairs add या update किए जा सकते हैं।

student = {
    "name": "Rahul",
    "age": 18
}

student.update({
    "age": 19,
    "city": "Jaipur"
})

print(student)

Output:

{'name': 'Rahul', 'age': 19, 'city': 'Jaipur'}

keys() Method

keys() method Dictionary की सभी keys का view देता है।

student = {
    "name": "Rahul",
    "age": 18,
    "city": "Jaipur"
}

print(student.keys())

values() Method

values() method Dictionary की सभी values का view देता है।

student = {
    "name": "Rahul",
    "age": 18,
    "city": "Jaipur"
}

print(student.values())

items() Method

items() method Dictionary के key-value pairs को view के रूप में देता है।

student = {
    "name": "Rahul",
    "age": 18
}

print(student.items())

Dictionary पर for Loop

Keys प्राप्त करना

student = {
    "name": "Rahul",
    "age": 18,
    "city": "Jaipur"
}

for key in student:
    print(key)

Keys और Values प्राप्त करना

student = {
    "name": "Rahul",
    "age": 18,
    "city": "Jaipur"
}

for key, value in student.items():
    print(key, ":", value)

Dictionary में Key Check करना

in operator की सहायता से check कर सकते हैं कि कोई key Dictionary में मौजूद है या नहीं।

student = {
    "name": "Rahul",
    "age": 18
}

if "name" in student:
    print("Name is available")

Output:

Name is available

pop() Method

pop() method specified key को remove करता है और उस key की value return करता है।

student = {
    "name": "Rahul",
    "age": 18,
    "city": "Jaipur"
}

age = student.pop("age")

print("Removed value:", age)
print(student)

Output:

Removed value: 18
{'name': 'Rahul', 'city': 'Jaipur'}

popitem() Method

popitem() Dictionary से एक key-value pair remove करके उसे return करता है।

student = {
    "name": "Rahul",
    "age": 18,
    "city": "Jaipur"
}

item = student.popitem()

print("Removed:", item)
print(student)

del Keyword

Dictionary से किसी particular key को हटाने के लिए del का उपयोग किया जा सकता है।

student = {
    "name": "Rahul",
    "age": 18
}

del student["age"]

print(student)

Output:

{'name': 'Rahul'}

clear() Method

clear() method Dictionary के सभी items को remove कर देता है।

student = {
    "name": "Rahul",
    "age": 18
}

student.clear()

print(student)

Output:

{}

Dictionary की Length

Dictionary में कुल key-value pairs की संख्या जानने के लिए len() function का उपयोग किया जाता है।

student = {
    "name": "Rahul",
    "age": 18,
    "city": "Jaipur"
}

print(len(student))

Output:

3

Dictionary में Duplicate Keys

Dictionary में एक key को unique रखना चाहिए। यदि same key दोबारा लिखी जाती है तो बाद वाली value पहले वाली value को replace कर सकती है।

student = {
    "name": "Rahul",
    "age": 18,
    "age": 19
}

print(student)

Output:

{'name': 'Rahul', 'age': 19}
Exam Tip: Dictionary में keys unique होती हैं, लेकिन values duplicate हो सकती हैं।

Dictionary में Duplicate Values

data = {
    "student1": "Rahul",
    "student2": "Rahul",
    "student3": "Amit"
}

print(data)

यह valid Dictionary है क्योंकि keys अलग-अलग हैं, भले ही दो keys की values समान हों।

Dictionary में Nested Dictionary

एक Dictionary के अंदर दूसरी Dictionary रखी जा सकती है। इसे Nested Dictionary कहा जाता है।

students = {
    "student1": {
        "name": "Rahul",
        "age": 18
    },
    "student2": {
        "name": "Amit",
        "age": 19
    }
}

print(students)

Nested Dictionary से Data Access

students = {
    "student1": {
        "name": "Rahul",
        "age": 18
    }
}

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

Output:

Rahul
18

Dictionary में List को Value के रूप में रखना

Dictionary की value के रूप में List भी रखी जा सकती है।

student = {
    "name": "Rahul",
    "subjects": ["Python", "Math", "English"]
}

print(student["subjects"])

Output:

['Python', 'Math', 'English']

Dictionary का Practical Example

नीचे student की basic information को Dictionary में store किया गया है।

student = {
    "name": "Rahul",
    "roll_no": 101,
    "class": 12,
    "percentage": 85.5
}

print("Name:", student["name"])
print("Roll No:", student["roll_no"])
print("Class:", student["class"])
print("Percentage:", student["percentage"])

Output:

Name: Rahul
Roll No: 101
Class: 12
Percentage: 85.5

Dictionary Methods Quick Table

Method उपयोग
keys() सभी keys प्राप्त करना
values() सभी values प्राप्त करना
items() Key-value pairs प्राप्त करना
get() Key से value प्राप्त करना
update() Items add या update करना
pop() Specified key को remove करना
popitem() एक key-value pair remove करना
clear() सभी items remove करना

Dictionary और List में अंतर

List Dictionary
Elements index के आधार पर access होते हैं। Values keys के आधार पर access होती हैं।
[ ] का उपयोग होता है। { } का उपयोग होता है।
Duplicate values रख सकती है। Keys unique होती हैं।
Data sequence के रूप में रखा जाता है। Data key-value pairs में रखा जाता है।

Dictionary और Set में अंतर

Set Dictionary
Unique elements का collection है। Key-value pairs का collection है।
सिर्फ elements होते हैं। Keys और values होती हैं।
{10, 20, 30} {"a": 10, "b": 20}
Indexing नहीं होती। Values keys से access की जाती हैं।

Dictionary Quick Revision

Concept मुख्य बात
Dictionary Key-value pair data structure
Key Unique identifier
Value Key से संबंधित data
keys() Keys प्राप्त करना
values() Values प्राप्त करना
items() Key-value pairs प्राप्त करना
get() Key से value प्राप्त करना
update() Dictionary को update करना
pop() Specified key remove करना
clear() सभी items remove करना

RBSE Class 12 Important Questions

  1. Python Dictionary क्या है?
  2. Dictionary की मुख्य विशेषताएँ लिखिए।
  3. Key-value pair क्या होता है?
  4. Dictionary में keys unique क्यों होती हैं?
  5. Dictionary में value को कैसे access करते हैं?
  6. get() method क्या है?
  7. keys() method का उपयोग बताइए।
  8. values() method का उपयोग बताइए।
  9. items() method का उपयोग बताइए।
  10. update() method क्या करता है?
  11. pop() method का उपयोग क्या है?
  12. popitem() method क्या करता है?
  13. clear() method का उपयोग बताइए।
  14. del keyword का उपयोग Dictionary में कैसे करते हैं?
  15. Dictionary और List में अंतर लिखिए।
  16. Dictionary और Set में अंतर लिखिए।
  17. Nested Dictionary क्या है?
  18. Dictionary में duplicate values हो सकती हैं या नहीं?
  19. Dictionary में duplicate keys का क्या होता है?
  20. Dictionary की length कैसे पता करते हैं?

Frequently Asked Questions (FAQs)

Python Dictionary क्या है?

Python Dictionary एक mutable data structure है जिसमें data को key-value pairs के रूप में store किया जाता है।

Dictionary में Key क्या होती है?

Key Dictionary में किसी value को identify और access करने के लिए उपयोग की जाने वाली identifier होती है।

क्या Dictionary में duplicate keys हो सकती हैं?

एक Dictionary में keys unique होनी चाहिए। एक ही key को दोबारा देने पर बाद वाली value पहले वाली value को replace कर सकती है।

Dictionary में कौन-कौन से important methods हैं?

keys(), values(), items(), get(), update(), pop(), popitem() और clear() प्रमुख Dictionary methods हैं।

Dictionary और Set में क्या अंतर है?

Set unique elements का collection है, जबकि Dictionary key-value pairs में data store करती है।

अब अगला Topic पढ़ें

RBSE Python Study Tip: Dictionary में सबसे पहले key-value concept समझें। इसके बाद keys(), values(), items(), get(), update(), pop() और clear() methods की practice करें। RBSE Class 12 के लिए इन methods के छोटे programs जरूर लिखकर practice करें।
Official Reference:

Python Dictionary की विस्तृत जानकारी के लिए Python की official documentation देखें।

Python Data Structures Documentation