You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is that possible use match case within list comprehension in current Python version? like this:
# When using `for ... in ... if ...` for current Python, you need lots of code after `if`.results= [city.nameforcityincitiesif (type(city) ==City) and (city.country=='BR')]
# Use `match case` instead of `if` in the list comprehensionresults= [nameforcityincitiesmatchcaseCity(country='BR', name=name)]
# To minimize the code, you don't need `case` here (just `match` keyword is enough)results= [nameforcityincitiesmatchCity(country='BR', name=name)]
# You can omit the `city` because you don't need it in this example (use `for _ in` or `for in`).results= [namefor_incitiesmatchCity(country='BR', name=name)]
results= [nameforincitiesmatchCity(country='BR', name=name)]
This would increase the readability and coding speed when you have lots logic to check. Maybe it also has chances to increase performance.
The syntax is more like SQL.
# final version (proposal)defmatch_brazil():
return [nameforincitiesmatchCity(country='BR', name=name)]
That's my proposal: for in match within list comprehension
The text was updated successfully, but these errors were encountered:
xareelee
changed the title
Proposal: for...match case for list comprehension
Proposal: for in match within list comprehension
Apr 14, 2022
Thanks for the feature proposal! However, ideas that would involve large changes to Python's syntax should be discussed at https://discuss.python.org/c/ideas/6 or on the python-ideas mailing list before opening an issue here, so I'm going to close this for now 🙂
Uh oh!
There was an error while loading. Please reload this page.
Just for discussion (I'm new to Python by the way). I saw an example for pattern matching with Class in Fluent Python (2nd Edition):
It uses 5 lines of code to filter the items in the list (actually, it is
filter
andmap
; orflatMap
in functional programming).Is that possible use
match case
within list comprehension in current Python version? like this:This would increase the readability and coding speed when you have lots logic to check. Maybe it also has chances to increase performance.
The syntax is more like SQL.
That's my proposal:
for in match
within list comprehensionThe text was updated successfully, but these errors were encountered: