3

I want to use Python's structural pattern matching to distinguish between a tuple (e.g. representing a point) and a list of tuples.

The straightforward approach does not work though:

def fn(p):
    match p:
        case (x, y):
            print(f"single point: ({x}, {y})")
        case [*points]:
            print("list of points:")
            for x, y in points:
                print(f"({x}, {y})")

fn((1, 1))
fn([(1, 1), (2, 2)])

which outputs:

single point: (1, 1)
single point: ((1, 1), (2, 2))

whereas I want it to output:

single point: (1, 1)
list of points:
(1, 1)
(2, 2)

Switching the order of the case statements also does not help here.

What is a good way to solve this with pattern matching?

mihi
  • 3,097
  • 16
  • 26

1 Answers1

6

Use tuple(foo) for matching a tuple and list(foo) for matching a list

def fn(p):
    match p:
        case tuple(contents):
            print(f"tuple: {contents}")
        case list(contents):
            print(f"list: {contents}")

fn((1, 1))  # tuple: (1, 1)
fn([(1, 1), (2, 2)])  # list: [(1, 1), (2, 2)]
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50