QA
Intermediate LevelPython

How does Structural Pattern Matching (match / case) in Python 3.10+ destructure complex dictionaries and objects?

❓ Interview Prompt: Explain Structural Pattern Matching (`match` / `case`), guard conditions (`if`), class pattern matching, and sequence destructuring.

Introduced in Python 3.10 (PEP 634), Structural Pattern Matching ('match' / 'case') allows matching and destructuring complex data structures simultaneously:

Features:

•
Destructures nested dictionaries, tuples, and custom class instances.
•
Supports Guard Conditions ('case ... if condition:').
•
Wildcard 'case _:' matches default fallback cases.

Code Implementation Example

python
def process_event(event):
    match event:
        case {'type': 'click', 'coords': (x, y)} if x > 0:
            print(f'Valid click at ({x}, {y})')
        case {'type': 'key', 'key': str(k)}:
            print(f'Key pressed: {k}')
        case _:
            print('Unknown event')
💡Destructuring dictionaries and tuples using match/case pattern matching.
🎯 Interview Key Takeaway

Use Structural Pattern Matching (match/case) in Python 3.10+ to destructure nested objects and replace long if-elif chains.

#match-case#Pattern Matching#Python 3.10#Intermediate
Back to all Python Questions→

More Python Interview Questions

Beginner LevelPython

What is the Python Global Interpreter Lock (GIL) and how does it affect multi-threading?

Beginner LevelPython

What is the difference between a List, Tuple, Set, and Dictionary in Python?

Beginner LevelPython

Why are mutable default arguments (like def func(items=[])) dangerous in Python and how do you fix them?