Matching Sequences and Mappings
Destructure lists and dicts.
Matching Sequences and Mappings is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Patterns That Destructure
The real power of match is destructuring. Sequence and mapping patterns let you check the shape of data and pull values out at the same time.
Matching a Fixed-Length List
A sequence pattern in square brackets matches a list or tuple of that exact length, binding each element to a name.
point = [3, 4]
match point:
case [x, y]:
print('x =', x, 'y =', y)Length Must Match
A two-element pattern does not match a three-element sequence. Each case checks both the structure and the length.
data = [1, 2, 3]
match data:
case [a, b]:
print('pair')
case [a, b, c]:
print('triple:', a, b, c)Mixing Literals and Captures
You can fix some positions to literals and capture others. This matches only sequences that start with the given literal.
command = ['move', 10, 20]
match command:
case ['move', dx, dy]:
print('moving by', dx, dy)
case ['stop']:
print('stopping')Capturing the Rest with *
A starred name captures any remaining elements as a list, much like extended unpacking.
items = [1, 2, 3, 4, 5]
match items:
case [first, *rest]:
print('first:', first)
print('rest:', rest)Empty and Single Patterns
Sequence patterns handle edge cases cleanly: an empty list, a single element, or many.
def describe(seq):
match seq:
case []:
return 'empty'
case [x]:
return 'one: ' + str(x)
case [x, *rest]:
return 'many, first ' + str(x)
print(describe([]))
print(describe([9]))
print(describe([1, 2, 3]))Matching Dictionaries
A mapping pattern uses braces. It checks that the given keys are present and binds their values. Extra keys in the subject are allowed.
event = {'type': 'click', 'x': 10, 'y': 20}
match event:
case {'type': 'click', 'x': x, 'y': y}:
print('click at', x, y)Partial Key Matching
A mapping pattern only requires the keys you list. Other keys are ignored, which is great for messages with optional fields.
event = {'type': 'key', 'code': 'A', 'extra': 1}
match event:
case {'type': 'key', 'code': code}:
print('key pressed:', code)Capturing Remaining Keys
Use **rest to capture all unmatched key-value pairs into a dictionary.
event = {'type': 'move', 'dx': 5, 'dy': 7}
match event:
case {'type': 'move', **rest}:
print('move details:', rest)Nesting Patterns
Patterns nest freely. You can match a dict that contains a list, destructuring both at once.
data = {'name': 'grid', 'size': [3, 4]}
match data:
case {'name': name, 'size': [w, h]}:
print(name, 'is', w, 'by', h)Strings Are Not Sequences Here
Sequence patterns deliberately do not match strings or bytes, even though they are iterable. This prevents a string from being accidentally destructured character by character.
value = 'hi'
match value:
case [a, b]:
print('matched as sequence')
case str():
print('matched as string')Quick Check
What does the pattern [first, *rest] bind when matched against [1, 2, 3, 4]?
Recap
You learned to destructure lists and dicts.
- Sequence patterns check length and bind elements.
*restcaptures remaining list items.- Mapping patterns match keys; extra keys are allowed.
**restcaptures remaining dict pairs; patterns nest.
Frequently asked questions
Is the “Matching Sequences and Mappings” lesson free?
Yes — the full text of “Matching Sequences and Mappings” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Matching Sequences and Mappings”?
Destructure lists and dicts. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Matching Sequences and Mappings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- match and case Basics
- Matching Sequences and Mappings
- Class Patterns
- Guards and Wildcards