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 :-

Is page me yeh sab sikhenge :-



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

Python
In [1]:
1
Output

Example 2: Value access karna — key se

Python
In [1]:
1
Output

Example 3: KeyError — galat key pe kya hota hai

Python
In [1]:
1
Output


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

Python
In [1]:
1
Output

Example 2: Items delete karna — del aur pop()

Python
In [1]:
1
Output

Example 3: update() — ek saath multiple keys update karna

Python
In [1]:
1
Output


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()

Python
In [1]:
1
Output

Example 2: setdefault(), copy() aur clear()

Python
In [1]:
1
Output

Example 3: fromkeys() — keys ki list se dict banana

Python
In [1]:
1
Output


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

Python
In [1]:
1
Output

Example 2: Loop se calculations — total marks aur average

Python
In [1]:
1
Output


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

Python
In [1]:
1
Output

Example 2: Nested dict me update karna aur loop chalana

Python
In [1]:
1
Output


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

Python
In [1]:
1
Output

Example 2: Condition ke saath — sirf passing marks wale

Python
In [1]:
1
Output

Example 3: Do lists se dictionary banana — zip() se

Python
In [1]:
1
Output



Quick Summary — Dictionary Methods ek nazar me

Method / OperationKaamExample
d[key]Value access — KeyError possibled["name"]
get(key, default)Safe value accessd.get("name", "N/A")
d[key] = valAdd ya updated["age"] = 25
update()Multiple add/updated.update({"a":1})
del d[key]Key delete karodel d["name"]
pop(key)Delete + return valued.pop("age")
popitem()Last item removed.popitem()
keys()Saari keysd.keys()
values()Saari valuesd.values()
items()key-value tuplesd.items()
setdefault()Key nahi ho toh default setd.setdefault("x", 0)
copy()Independent copyd2 = d.copy()
clear()Sab hataod.clear()
fromkeys()Keys list se dict bananadict.fromkeys(lst, 0)