Python Dictionaries - Advanced Guide with Examples (Hinglish)

Introduction

Dictionary Python ka ek hash map jaisa data structure hai jo key-value pairs me data store karta hai. Ye super-fast lookup deta hai aur real-world projects me kaafi useful hai, specially jab data ko uniquely identify karna hota hai.

Dictionary Create Karne ke Different Ways

# Method 1: Curly braces
person1 = {"name": "Amit", "age": 30}

# Method 2: dict() constructor
person2 = dict(name="Neha", age=25)

# Method 3: from list of tuples
person3 = dict([("name", "Raj"), ("age", 28)])

# Method 4: Dictionary comprehension
squares = {x: x*x for x in range(1,6)}
print(squares)  # {1:1, 2:4, 3:9, 4:16, 5:25}
    

Keys and Values - Advanced Details

Safe Access with get() & setdefault()

student = {"name": "Arun", "age": 21}

print(student.get("grade", "Not Assigned"))  # Default value de sakte ho
# Agar grade nahi mila to "Not Assigned" return hoga

student.setdefault("grade", "Pending")
print(student)  
# {'name':'Arun','age':21,'grade':'Pending'}
    

Dictionary Update Tricks

info = {"a": 1, "b": 2}
extra = {"b": 3, "c": 4}

info.update(extra)   # merge/update dict
print(info)  # {'a':1, 'b':3, 'c':4}
    

Deleting Elements

data = {"x": 10, "y": 20, "z": 30}

val = data.pop("y")   # delete & return
print(val)            # 20

last = data.popitem() # remove last inserted
print(last)           # ('z', 30)

data.clear()          # puri dict empty kar do
print(data)           # {}
    

Checking Existence

student = {"name": "Karan", "age": 19}
print("name" in student)   # True
print("grade" not in student) # True
    

Looping Advanced Examples

person = {"name":"Ravi", "age":24, "city":"Delhi"}

# Keys loop
for k in person.keys():
    print("Key:", k)

# Values loop
for v in person.values():
    print("Value:", v)

# Items loop
for k, v in person.items():
    print(f"{k} -> {v}")
    

Nested Dictionaries with Real Example

company = {
  "emp1": {"name":"Rahul", "dept":"IT", "skills":["Python","SQL"]},
  "emp2": {"name":"Sneha", "dept":"HR", "skills":["Excel","Recruitment"]}
}

print(company["emp1"]["skills"][0])  # Python
    

Dictionary Comprehension - Advanced

# Mapping numbers to even/odd
nums = {x: ("Even" if x%2==0 else "Odd") for x in range(1,8)}
print(nums)
# {1:'Odd', 2:'Even', 3:'Odd', ...}
    

Practical Examples

1. Word Frequency Counter

text = "apple banana apple orange banana apple"
counter = {}

for word in text.split():
    counter[word] = counter.get(word, 0) + 1

print(counter)
# {'apple':3, 'banana':2, 'orange':1}
    

2. Student Record Management

students = {
  101: {"name":"Anita","marks":85},
  102: {"name":"Vikram","marks":92},
  103: {"name":"Kiran","marks":78}
}

rollno = 102
print(students[rollno]["name"])   # Vikram
    

Dictionary vs Other Data Structures

Summary

Dictionaries Python me ek super flexible aur optimized data structure hai jo real-world programming me bohot important hai. Agar aapko uniquely key-value relation maintain karna hai, to dictionary best choice hoti hai. Basic se leke advanced methods jaanna important hai taaki aap efficiently kaam kar saken.