1. Dictionary Basics Definition: An unordered collection of key-value pairs. Keys must be unique and immutable (strings, numbers, tuples). Values can be any data type. Creating a Dictionary: Empty dictionary: my_dict = {} With initial values: my_dict = {'name': 'Alice', 'age': 30} Using dict() constructor: my_dict = dict(name='Bob', age=25) From a list of key-value pairs: my_dict = dict([('a', 1), ('b', 2)]) 2. Accessing & Modifying Elements Accessing Values: Using keys: value = my_dict['key'] (raises KeyError if key not found) Using .get() method: value = my_dict.get('key') (returns None if key not found) With default value: value = my_dict.get('key', 'default_value') Adding/Updating Elements: my_dict['new_key'] = new_value (adds if key doesn't exist, updates if it does) my_dict.update({'key1': 'value1', 'key2': 'value2'}) (merges dictionaries) Deleting Elements: del my_dict['key'] (deletes specific key-value pair) popped_value = my_dict.pop('key') (removes key and returns its value) popped_item = my_dict.popitem() (removes and returns an arbitrary key-value pair as a tuple, Python 3.7+ order-preserving) my_dict.clear() (removes all items from the dictionary) 3. Dictionary Methods .keys() : Returns a view object that displays a list of all the keys in the dictionary. .values() : Returns a view object that displays a list of all the values in the dictionary. .items() : Returns a view object that displays a list of a dictionary's key-value tuple pairs. .copy() : Returns a shallow copy of the dictionary. .setdefault(key, default_value) : Returns the value of the key. If the key does not exist, it inserts the key with the default_value . dict.fromkeys(iterable, value) : Creates a new dictionary with keys from iterable and values set to value (defaults to None ). 4. Iteration & Membership Iterating through keys: for key in my_dict: print(key) for key in my_dict.keys(): print(key) Iterating through values: for value in my_dict.values(): print(value) Iterating through key-value pairs: for key, value in my_dict.items(): print(f"{key}: {value}") Checking for key existence: 'key' in my_dict (returns True or False ) 'key' not in my_dict 5. Dictionary Comprehension Basic form: new_dict = {key_expr: value_expr for item in iterable} Example: squares = {x: x*x for x in range(5)} # Result: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} With conditional logic: even_squares = {x: x*x for x in range(10) if x % 2 == 0} # Result: {0: 0, 2: 4, 4: 16, 6: 36, 8: 64} 6. Common Use Cases Counting Frequencies: word_counts = {} words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'] for word in words: word_counts[word] = word_counts.get(word, 0) + 1 # Result: {'apple': 3, 'banana': 2, 'orange': 1} Mapping Data: Storing configurations, user profiles, or lookup tables. Grouping Data: Grouping items by a common attribute. 7. Immutability of Keys Keys must be hashable (immutable). Examples: strings, numbers, tuples. Mutable types like lists, sets, or other dictionaries cannot be used as keys. Valid Key: my_dict[(1, 2)] = 'tuple_key' Invalid Key: # my_dict[[1, 2]] = 'list_key' (raises TypeError: unhashable type: 'list' ) 8. Dictionary View Objects .keys() , .values() , .items() return view objects. These views provide a dynamic view of the dictionary's contents. If the dictionary changes, the view reflects those changes. They are not lists; to get a list, cast them explicitly: list_of_keys = list(my_dict.keys())