Dictionaries in Python
Master Python dictionaries (maps): creation, access, mutation, traversal, comparison, copying, merging, dictionary comprehension, and practical usage patterns.
Introduction to Dictionaries
A dictionary is a key-value data structure used to map unique keys to values.
1. What is a Dictionary?
Key-value mapping.
Creating Dictionaries
Dictionaries can be created using literals or the dict() constructor.
1. Dictionary Literal
Recommended way.
2. dict() Constructor
Using keyword arguments.
Accessing & Updating Values
Dictionary values can be accessed using keys.
1. Access by Key
Use square brackets or get().
2. Updating Values
Modify existing keys.
Adding & Deleting Keys
Dictionaries are dynamic and can grow or shrink.
1. Adding Key
Assign new key.
2. Deleting Key
Use del or pop().
Traversing Dictionaries
Loop through keys, values, or key-value pairs.
1. Loop Through Keys
Iterate keys only.
2. Loop Through Key-Value Pairs
Use items().
Comparing Dictionaries
Dictionaries compare by value, not reference.
1. Value Comparison
Use == operator.
Copying Dictionaries
Understand shallow vs deep copy.
1. Shallow Copy
Use copy().
2. Deep Copy
Use copy module.
Merging Dictionaries
Combine multiple dictionaries.
1. Using | Operator (Python 3.9+)
Merge two dicts.
Dictionary Comprehension
Create dictionaries dynamically using comprehension.
1. Basic Transformation
Create key-value pairs.
2. Conditional Dictionary
Filter inside comprehension.
Dictionary Unpacking
Unpacking allows extracting and spreading key-value pairs into new dictionaries, function calls, and assignments.
1. Basic ** Unpacking
Expand dictionary into another dictionary.
2. Overriding Keys During Unpacking
Later values overwrite earlier keys.
3. Creating a Shallow Copy
Using unpacking to copy.
4. Unpacking into Function Arguments
Pass dictionary as named parameters.
5. Mixing Fixed Keys and Unpacked Keys
Combine manual keys with unpacked dictionary.
Dictionary Pattern Matching (Python 3.10+)
Pattern matching allows structural destructuring of dictionaries in a safe and expressive way.
1. Basic Dictionary Pattern
Extract values directly from structure.
2. Partial Matching
Match only some keys.
3. Pattern with Guard Condition
Add extra validation.
4. Nested Dictionary Matching
Match deep structures.
5. Using Wildcards (_)
Ignore values you do not care about.
Additional Dictionary Operations
These are important built-in dictionary methods commonly used in real applications.
1. keys(), values(), items()
Retrieve dictionary views.
2. pop() and popitem()
Remove and return values.
3. setdefault()
Insert key only if missing.
4. update()
Modify dictionary in-place.
5. fromkeys()
Create dictionary from keys.
Sorting Dictionaries
Dictionaries themselves are not sorted automatically, but can be sorted using sorted().
1. Sort by Keys
Use sorted() on keys.
2. Sort by Values
Use sorted() with key parameter.
Specialized Dictionary Types
Python provides advanced dictionary-like data structures for special use cases.
1. defaultdict
Automatic default values.
2. Counter
Count occurrences easily.
Hashing Rules for Dictionary Keys
Understanding why some objects cannot be dictionary keys is critical.
1. Why Keys Must Be Immutable
Hashable requirement.