I want to write a function in Python that returns different fixed values based on the value of an input index.
In other languages I would use a switch or case statement, but Python does not appear to have a switch statement. What are the recommended Python solutions in this scenario?
Python 3.10 (2021) introduced the
match–casestatement, which provides a first-class implementation of a "switch" for Python. For example:The
match–casestatement is considerably more powerful than this simple example.If you need to support Python ≤ 3.9, use a dictionary instead: