Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

354
Vistas
How to distinguish between a tuple and a list in Python's structural pattern matching?

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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)]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda