Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

352
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!