Python me Dictionaries kya hoti hain?
Dictionary Python ka ek key-value pair collection hai. Jaise ek real dictionary me word (key) hota hai aur uska meaning (value) — exactly waise hi Python dict me bhi kaam hota hai. Aap key ke zariye directly value access kar sakte hain — koi index dhundhne ki zarurat nahi.
Dictionary ki 5 main khasiyatein hain :-
- Key-Value Pairs — har item ek key aur value ka joda hota hai
- Ordered — Python 3.7+ me insertion order maintain hota hai
- Mutable — items add, update ya remove ho sakte hain
- Keys Unique — ek key sirf ek baar ho sakti hai
- Keys Immutable — key string, number ya tuple ho sakti hai (list nahi)
Is page me yeh sab sikhenge :-
- 1. Dictionary banana aur access karna
- 2. Items add, update aur delete karna
- 3. Dictionary Methods — keys, values, items, get, pop, update
- 4. Dictionary ke saath loops
- 5. Nested Dictionary
- 6. Dictionary Comprehension
1. Dictionary Banana aur Access Karna
Dictionary ko curly brackets { } me key: value pairs likh ke banate hain. Keys aur values ko colon ( : ) se alag kiya jaata hai, aur pairs ko comma ( , ) se.
Example 1: Dictionary banana — basic
Example 2: Value access karna — key se
Example 3: KeyError — galat key pe kya hota hai
2. Items Add, Update aur Delete Karna
Dictionary mutable hai — isme baad me bhi items add, update ya delete kiye ja sakte hain.
Example 1: Naya item add karna aur existing update karna
Example 2: Items delete karna — del aur pop()
Example 3: update() — ek saath multiple keys update karna
3. Dictionary Methods
Python dict ke saath bahut saare built-in methods aate hain jo kaam ko aasaan banate hain.
Example 1: keys(), values() aur items()
Example 2: setdefault(), copy() aur clear()
Example 3: fromkeys() — keys ki list se dict banana
4. Dictionary ke saath Loops
Dictionary pe for loop chalane ke 3 tarike hain — sirf keys, sirf values, ya dono saath items() se.
Example 1: Keys, Values aur Items pe loop
Example 2: Loop se calculations — total marks aur average
5. Nested Dictionary
Dictionary ke andar doosri dictionary — isse Nested Dictionary kehte hain. Real applications me yeh bahut use hota hai — jaise user profiles, product catalog, JSON data, etc.
Example 1: Nested dict banana aur access karna
Example 2: Nested dict me update karna aur loop chalana
6. Dictionary Comprehension
List comprehension ki tarah Dictionary Comprehension se hum ek hi line me nai dictionary bana sakte hain — loop aur condition ke saath.
| Syntax |
|---|
{key: value for item in iterable if condition}
|
Example 1: Squares dictionary — number: uska square
Example 2: Condition ke saath — sirf passing marks wale
Example 3: Do lists se dictionary banana — zip() se
Quick Summary — Dictionary Methods ek nazar me
| Method / Operation | Kaam | Example |
|---|---|---|
| d[key] | Value access — KeyError possible | d["name"] |
| get(key, default) | Safe value access | d.get("name", "N/A") |
| d[key] = val | Add ya update | d["age"] = 25 |
| update() | Multiple add/update | d.update({"a":1}) |
| del d[key] | Key delete karo | del d["name"] |
| pop(key) | Delete + return value | d.pop("age") |
| popitem() | Last item remove | d.popitem() |
| keys() | Saari keys | d.keys() |
| values() | Saari values | d.values() |
| items() | key-value tuples | d.items() |
| setdefault() | Key nahi ho toh default set | d.setdefault("x", 0) |
| copy() | Independent copy | d2 = d.copy() |
| clear() | Sab hatao | d.clear() |
| fromkeys() | Keys list se dict banana | dict.fromkeys(lst, 0) |