Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proposal: for in match within list comprehension #91540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xareelee opened this issue Apr 14, 2022 · 1 comment
Closed

Proposal: for in match within list comprehension #91540

xareelee opened this issue Apr 14, 2022 · 1 comment

Comments

@xareelee
Copy link

xareelee commented Apr 14, 2022

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):

class City(typing.NamedTuple):
    continent: str
    name: str
    country: str

cities = [
    City('Asia', 'Tokyo', 'JP'),
    City('Asia', 'Delhi', 'IN'),
    City('North America', 'Mexico City', 'MX'),
    City('North America', 'New York', 'US'),
    City('South America', 'São Paulo', 'BR'),
]

def match_brazil():
    results = []
    for city in cities:
        match city:
            case City(country='BR', name=name):
                results.append(name)
    return results

It uses 5 lines of code to filter the items in the list (actually, it is filter and map; or flatMap in functional programming).

results = []
for city in cities:
    match city:
        case City(country='BR', name=name):
            results.append(name)

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.name for city in cities if (type(city) == City) and (city.country == 'BR')]

# Use `match case` instead of `if` in the list comprehension
results = [name for city in cities match case City(country='BR', name=name)]

# To minimize the code, you don't need `case` here (just `match` keyword is enough)
results = [name for city in cities match City(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 = [name for _ in cities match City(country='BR', name=name)]
results = [name for in cities match City(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)
def match_brazil():
    return [name for in cities match City(country='BR', name=name)]

That's my proposal: for in match within list comprehension

@xareelee xareelee changed the title Proposal: for...match case for list comprehension Proposal: for in match within list comprehension Apr 14, 2022
@AlexWaygood
Copy link
Member

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 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants