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

197
Views
Defining a match-case statement with the same instruction for two different cases in Python

I would like to create a match-case statement in Python in which two cases do the same instruction. For instance, if Python behaved like C++, according to this Stack Overflow question, I would write the match-case statement as:

        match x:
            case 1:
            case 2:
                # Some code
                break
            case 3:
                # Some code
                break

The code however does not work, showing that the match-case statement needs to be written in a different way in Python. What is this way?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

match x:
    case 1 | 2:
        # Some code
    case 3:
        # Some code

Python match clauses don't fall through, so you can't concatenate them as in C and you don't use break at the end of the clause.

The "OR pattern" (as shown, where | means "or") can be used with subpatterns which bind variables, but all the subpatterns need to bind the same variables.

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!