Description
Feature or enhancement
Note
(by @picnixz): Feature is under DPO consideration. Issue will be re-opened once sufficient support has been gathered (and likely a PEP has been accepted).
Proposal:
While list indexes can be written to match to other variables or constants, list indexes unfortunately currently cannot be cases to be matched with. Additionally, this dynamic sort of feature also already exists in JavaScript; although, it actually uses simpler value matching instead of languages like Python or C# that make use of structural pattern matching.
If addressed, Python developers will be able to simply edit a list to change case clauses without having to scroll through each and every one to find the one they'd like to change.
The use could potentially resemble the following example code…
lst = ['a', 'b', 'c'] # of options in this example
user_input = input(f'Input {lst[0]}, {lst[1]}, or {lst[2]}. \n: ').strip().lower()
match user_input:
case lst[0]:
print('\nFirst option chosen. ')
...
case lst[1]:
print('\nSecond option chosen. ')
...
case lst[2]:
print('\nThird option chosen. ')
...
case _:
print('\nInvalid input. Kindly try again.')
...
# shows a SyntaxError
However, as mentioned here later, someone suggested an edit in the above example code and provided the following code block — with the difference being the addition of ==
, which can be is
instead if appropriate…
lst = ['a', 'b', 'c'] # of options in this example
user_input = input(f'Input {lst[0]}, {lst[1]}, or {lst[2]}. \n: ').strip().lower()
match user_input:
case == lst[0]:
print('\nFirst option chosen. ')
...
case == lst[1]:
print('\nSecond option chosen. ')
...
case == lst[2]:
print('\nThird option chosen. ')
...
case _:
print('\nInvalid input. Kindly try again.')
...
They also highlighted how this addition had "an obvious (and accurate) mapping" to a version of the example code using if
-else
statements, for which they provided the following edition continuing after declarations…
if user_input == lst[0]:
print('\nFirst option chosen. ')
...
elif user_input == lst[1]:
print('\nSecond option chosen. ')
...
elif user_input == lst[2]:
print('\nThird option chosen. ')
...
else:
print('\nInvalid input. Kindly try again.')
...
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
LinkedIn Post: https://www.linkedin.com/posts/profile-of-maha-ijaz_serious-careerchoice-humour-activity-7278731541126524929-6jfI?utm_source=share
The image attached to the post describes how JavaScript already has this feature in its switch-case statements while the latest stable version of Python (3.13.x) does not.
Python discussion: https://discuss.python.org/t/case-clauses-should-be-able-to-match-list-indexes/75540
Opened after issue — sorry.
Summary of state as of December 30, 2024:
- Two links presented, one to this same GitHub (GH) issue and another to the Stack Overflow (SO) question which is mentioned here afterwards as well.
- A core CPython developer expressed approval and also suggested a subfeature from PEP 642, which that same person authored. The subfeature in question allows for
==
andis
to be used as prefix operations to replace implicit value patterns in this case.
Old but related SO question:
https://stackoverflow.com/questions/66159432/how-to-use-values-stored-in-variables-as-case-patterns
In this, one account even said,
that's been proposed for Python (and rejected) twice before …
It's interesting to see how many times this exact topic has been brought up or revisited.