BCA / B.Tech 9 min read

Dictionary in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Python Notes in Hindi | पायथन नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Dictionary in Hindi - General Topics
  • • अवलोकन: Quick Answer: Python mein Dictionary ek built-in, mutable data structure hai jo data ko Key-Value pairs ke roop mein store karti hai. Iska syntax curly ...
  • • मुख्य अवधारणाएं: Python dictionary, Key value pairs access update Hindi, Delete nesting dict,Python dictionary, Key value pairs access update, Delete nesting dict
Quick Answer: Python mein Dictionary ek built-in, mutable data structure hai jo data ko Key-Value pairs ke roop mein store karti hai. Iska syntax curly braces {} hota hai (jaise user = {"name": "Aman", "age": 22}). Har key unique aur immutable (hashable jaise string, number, ya tuple) honi chahiye, jabki values kisi bhi data type ki aur duplicate ho sakti hain. Python 3.7 se dictionaries insertion order preserve karti hain aur hashing mechanism ke karan average O(1) time complexity par fast search provide karti hain.

Python Dictionary kya hai aur iska Concept

Python mein basic sequence types (jaise list aur tuple) elements ko 0-indexed positions se access karte hain. Lekin real-world data aksar labeled hota hai (jaise roll number ke sath student details ya username ke sath profile data). Numeric index ki jagah descriptive meaningful labels se data store karne ke liye Python Dictionary (dict) provide karta hai.

Python ke anya data structures samajhne ke liye hamara List and Tuples in Hindi aur Data Types in Python in Hindi guide padhein.

Advertisement

Python 3.7+ mein Insertion Order: Ek Mahatvapurna Fact

Purani Python versions (Python 3.5 aur usse pehle) mein dictionaries completely unordered hoti thi; items print karne par unka order random ho sakta tha. Python 3.6 (CPython) mein yeh order ek implementation detail tha, lekin Python 3.7 se yeh language ki official guarantee ban gaya, aur ab dictionaries items ke insert hone ke sequence (insertion order) ko guarantee ke sath preserve karti hain. Lekin fir bhi elements ko index number (jaise dict[0]) se access nahi kiya jata; inhe kewal unki assigned keys se access kiya jata hai.

Dictionary Create karne ke 3 Tarike

1. Curly Braces {} ka use karke

# Standard syntax
student = {
    "roll_no": 101,
    "name": "Pooja Sharma",
    "course": "BCA",
    "marks": 88.5
}
print(student)

2. dict() Constructor ka use karke

# Keyword arguments ke zariye
employee = dict(empCode=501, name="Vikram", salary=45000, department="IT")
print(employee)

3. Dictionary Comprehension

Short concise syntax se dynamic dictionary generate karna:

# Numbers aur unke squares ki dictionary
squares = {num: num ** 2 for num in range(1, 6)}
# Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print(squares)

Values Access karna: Bracket Notation vs get() Method

Dictionary se value fetch karne ke do popular tarike hote hain:

  • Square Bracket Syntax (dict[key]): Agar key dictionary mein exist karti hai toh value mil jaati hai. Lekin agar key maujood na ho, toh Python KeyError throw karke program crash kar deta hai.
  • get() Method: Yah safe method hai. Agar key exist nahi karti, toh error aane ki jagah None ya user-defined default fallback value return hoti hai.
profile = {"username": "tech_guru", "country": "India"}

# Square brackets
print(profile["username"]) # Output: tech_guru
# print(profile["age"])    # KeyError crash!

# Safe get method
print(profile.get("age"))                 # Output: None
print(profile.get("age", "Not Specified")) # Output: Not Specified

Python Collection Data Types ka Comparison Matrix

Data Structure Syntax / Brackets Ordered (3.7+) Mutability Duplicate Elements Indexing Method
List Square brackets [] Yes Mutable (Changeable) Duplicates Allowed Zero-based Integer Index
Tuple Parentheses () Yes Immutable (Read-only) Duplicates Allowed Zero-based Integer Index
Set Curly braces {} No (Unordered) Mutable Unique Only (No Duplicates) Unindexed (No direct index)
Dictionary Key-Value in {k: v} Yes (Insertion Order) Mutable Keys Unique; Values Duplicates Key-based Lookup

Pramukh Built-in Dictionary Methods

Dictionary manipulation ke liye Python kai useful built-in functions provide karta hai. Built-in methods ki complete list ke liye Dictionary Built-in Methods in Hindi padhein:

Method Name Syntax Example Operation & Description
keys() d.keys() Dictionary ki sabhi keys ka view object return karta hai.
values() d.values() Dictionary ki sabhi values ka view object return karta hai.
items() d.items() Har key-value pair ko (key, value) tuple ke form mein return karta hai (looping ke liye best).
update() d.update({"city": "Jaipur"}) Naye key-value pairs add karta hai ya existing keys ko overwrite karta hai.
pop() d.pop("age") Specified key ko remove karta hai aur uski value return karta hai.
popitem() d.popitem() Aakhiri insert kiya gaya (key, value) pair remove aur return karta hai.
clear() d.clear() Dictionary ke sabhi elements delete karke use completely empty {} bana deta hai.
Advertisement

Dictionary Looping aur Nested Dictionaries Example

Dictionary ke data ko iterate karna Python programming ka ek common task hai:

# Nested Dictionary: Organization ke employees ka data
company = {
    "emp101": {"name": "Sunil", "role": "Developer", "exp": 3},
    "emp102": {"name": "Megha", "role": "Designer", "exp": 2}
}

# items() method se iterate karna
for empKey, empInfo in company.items():
    print("Employee Code:", empKey)
    for field, value in empInfo.items():
        print(f"  {field} -> {value}")

Dictionary ke Under-the-Hood Mechanics: Hash Table & Immutable Keys

Python dictionary internal architecture mein Hash Table use karti hai. Jab aap myDict["name"] = "Amit" likhte hain, toh Python internal hash("name") function call karke ek hash integer generate karta hai (do alag keys ka hash kabhi same bhi ho sakta hai, ise collision kehte hain aur Python ise khud handle karta hai). Is integer address ke zariye value memory slot mein direct point ho jaati hai.

Isi wajah se dictionary lookup average time mein O(1) Constant Time complexity leta hai. Sath hi, yahi karan hai ki dictionary key hamesha Immutable (aprivartansheel) honi chahiye (jaise String, Integer ya Tuple). Yadi aap mutable object (jaise List) ko key banane ki koshish karenge, toh Python TypeError: unhashable type: 'list' error throw karega.

Exam के लिए Important Questions

Short Questions

  1. Python Dictionary kya hai? Iski do mukhya visheshataayein likhiye.
    Dictionary ek mutable data structure hai jo Key-Value pairs mein data store karti hai. Iski keys unique hoti hain aur lookup speed O(1) hoti hai.
  2. Dictionary ki Key ke roop mein kaunse data types use kiye ja sakte hain?
    Kewal immutable aur hashable data types jaise String, Integer, Float aur Tuple (bina mutable elements ke) key ban sakte hain.
  3. pop() aur popitem() methods mein kya antar hai?
    pop(key) specific key ko remove karke uski value deta hai, jabki popitem() dictionary ka sabse aakhiri inserted item (LIFO order) remove karta hai.
  4. Kya Python 3.7 ke baad Dictionary ordered hoti hai?
    Ha, Python 3.7+ mein dictionary items ke insertion order ko strictly preserve karti hai.

Long Questions

  1. Python Dictionary kya hai? Iske syntax, internal hash-table architecture aur list/tuple ke sath tulnatmak vishleshan ko samjhaiye.
  2. Dictionary ke pramukh built-in methods (keys, values, items, get, update, pop) ko suitable code snippets aur outputs ke sath describe karein.
  3. Dictionary Comprehension aur Nested Dictionaries kya hain? Inka prayog karte huye ek complex real-world data filtering script likhiye.

Frequently Asked Questions (FAQs)

Q1: Kya do alag keys ki same value ho sakti hai?
Ans: Ha, dictionary values duplicate ho sakti hain (jaise {"student1": 85, "student2": 85}), lekin keys hamesha unique honi chahiye.

Q2: Agar hum duplicate key insert karein toh kya hoga?
Ans: Python koi error throw nahi karega, balki purani value ko nayi value se overwrite kar dega (jaise {"a": 1, "a": 2} ka result {"a": 2} hoga).

Q3: Key exist karti hai ya nahi, ise check karne ka best tarika kya hai?
Ans: in membership operator use karein (jaise if "name" in student:). Yeh direct true ya false return karta hai bina kisi error ke.

Q4: Do dictionaries ko Python mein merge kaise karte hain?
Ans: Python 3.9+ mein merge operator | use kar sakte hain (jaise merged = dict1 | dict2), ya purani versions mein dict1.update(dict2) use hota hai.

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Python mein Dictionary ek built-in, mutable data structure hai jo data ko Key-Value pairs ke roop mein store karti hai.
Python Notes in Hindi | पायथन नोट्स हिंदी में के पाठ्यक्रम में Dictionary in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Dictionary in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement