> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Sequence Pattern

A sequence pattern is a structural pattern matching construct introduced in Python 3.10 that unpacks and matches a subject against a sequence of sub-patterns. It verifies that the subject is an instance of `collections.abc.Sequence` and that its elements satisfy the specified positional sub-patterns.

Notably, sequence patterns explicitly exclude `str`, `bytes`, and `bytearray` objects to prevent unintended character-level or byte-level unpacking.

## Syntax and Equivalence

Sequence patterns can be defined using either square brackets `[]` or parentheses `()`. At the compiler level, both syntaxes are evaluated identically; `[a, b]` and `(a, b)` represent the exact same pattern.

```python theme={"dark"}
match subject:
    case [pattern_1, pattern_2]:
        # Matches a sequence of exactly length 2
    case (pattern_1, pattern_2, pattern_3):
        # Matches a sequence of exactly length 3
```

## Length and Element Matching

By default, a sequence pattern enforces a strict length check. The subject sequence must contain the exact number of elements as the sub-patterns defined in the `case` clause. Each element in the subject is then evaluated against the corresponding sub-pattern from left to right.

```python theme={"dark"}
match subject:
    case [1, x, 3]:
        # Matches a sequence of length 3 where index 0 is 1 and index 2 is 3.
        # The value at index 1 is bound to the variable 'x'.
    case [_, _]:
        # Matches any sequence of exactly length 2. 
        # The wildcard '_' discards the values.
```

## Variable-Length Matching (Starred Expressions)

To match sequences of arbitrary lengths, sequence patterns support a single starred expression (`*`), similar to iterable unpacking. The starred name binds to a `list` containing all remaining elements that are not matched by the positional sub-patterns.

```python theme={"dark"}
match subject:
    case [first, *middle, last]:
        # Matches a sequence of length >= 2.
        # Binds index 0 to 'first', the final index to 'last', 
        # and packs all intermediate elements into a list bound to 'middle'.
    case [*_, final_element]:
        # Matches a sequence of length >= 1.
        # Discards all elements except the last one, which is bound to 'final_element'.
```

## Nested Sub-patterns

Sequence patterns are recursive. The sub-patterns within the sequence can be literal patterns, capture patterns, type patterns, or other structural patterns (including nested sequence or mapping patterns).

```python theme={"dark"}
match subject:
    case [int(x), [str(y), z]]:
        # Matches a sequence of length 2 where:
        # 1. The first element is an integer (bound to 'x').
        # 2. The second element is a nested sequence of length 2.
        # 3. The first element of the nested sequence is a string (bound to 'y').
        # 4. The second element of the nested sequence is bound to 'z'.
```

## Binding the Entire Sequence

The `as` pattern can be combined with a sequence pattern to capture the entire matched sequence object into a variable, while simultaneously unpacking its elements.

```python theme={"dark"}
match subject:
    case [a, b, c] as full_sequence:
        # If the subject is a sequence of length 3, elements are bound to a, b, and c.
        # The original subject object itself is bound to 'full_sequence'.
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
