1. Creating Dictionaries
1.1 Empty Dictionary
my_dict = {}1.2 Dictionary with Initial Values
my_dict = {"name": "Alice", "age": 30}1.3 Dictionary with Mixed Data Types
mixed_dict = {"number": 42, "text": "hello", "list": [1, 2, 3], "flag": True}1.4 Nested Dictionaries
nested_dict = {
"person1": {"name": "Alice", "age": 25},
"person2": {"name": "Bob", "age": 30}
}2. Accessing Elements
2.1 Access by Key
print(my_dict["name"]) # Output: Alice2.2 Safely Access Using .get()
print(my_dict.get("name")) # Output: Alice
print(my_dict.get("profession", "Unknown")) # Output: Unknown if not present3. Modifying Dictionaries
3.1 Add or Update an Element
my_dict["profession"] = "Engineer" # Adds a new key or updates if exists3.2 Remove Elements
del my_dict["age"] # Removes the key 'age'
profession = my_dict.pop("profession", "No profession found") # Removes and returns
my_dict.clear() # Clears all elements4. Dictionary Operations
4.1 Check if Key Exists
"name" in my_dict # Returns True if 'name' is a key4.2 Iterate Through Keys, Values, or Items
for key in my_dict.keys():
print(key)
for value in my_dict.values():
print(value)
for key, value in my_dict.items():
print(f"{key}: {value}")4.3 Dictionary Comprehensions
squared = {x: x**2 for x in range(5)} # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}4.4 Merge Dictionaries
dict1 = {"name": "Alice", "age": 25}
dict2 = {"city": "New York", "age": 30}
merged = {**dict1, **dict2} # Python 3.5+ method5. Common Dictionary Methods
5.1 Get Dictionary Length
len(my_dict) # Returns the number of key-value pairs5.2 Copy a Dictionary
new_dict = my_dict.copy() # Creates a shallow copy of the dictionary5.3 Get All Keys or Values
all_keys = list(my_dict.keys())
all_values = list(my_dict.values())5.4 Update Dictionary
my_dict.update({"age": 26, "city": "Boston"}) # Updates and adds multiple keys5.5 Set Default Value for Key
my_dict.setdefault("age", 29) # Sets 'age' to 29 if key is not present6. Common Dictionary Pitfalls
6.1 Avoid Modifying a Dictionary While Iterating
# Incorrect
for key in my_dict:
if key.startswith('a'):
del my_dict[key]
# Correct (Using a copy of keys)
for key in list(my_dict.keys()):
if key.startswith('a'):
del my_dict[key]