Python Dictionary Guide
Python Dictionary Guide
1. What is a Dictionary?
------------------------
A dictionary in Python is a collection of unordered, mutable, and indexed k
Each key-value pair is separated by a colon (:) and each pair is separated b
Syntax:
--------
my_dict = {
"name": "John",
"age": 30,
"city": "New York"
}
Key Features:
--------------
1. Unordered
2. Mutable
3. Indexed
4. Keys must be immutable
3. Modifying Items
------------------
- Change Value:
my_dict["age"] = 31
- Using update():
my_dict.update({"age": 32})
4. Adding Items
---------------
my_dict["gender"] = "Male"
5. Removing Items
-----------------
- del:
del my_dict["city"]
- pop():
my_dict.pop("age")
- popitem():
my_dict.popitem()
6. Dictionary Methods
----------------------
- keys(), values(), items()
- clear(), copy(), fromkeys()
8. Advanced Operations
-----------------------
- Nested dictionaries
- Dictionary comprehension
- Merging dictionaries