|
|
Subscribe / Log in / New account

Corner cases and exception types

Corner cases and exception types

Posted Aug 21, 2019 20:27 UTC (Wed) by smurf (subscriber, #17840)
In reply to: Corner cases and exception types by robert_s
Parent article: Corner cases and exception types

So how would you rewrite the "filtered_data" example? It's plenty concise for me.


to post comments

Corner cases and exception types

Posted Aug 22, 2019 16:34 UTC (Thu) by nybble41 (subscriber, #55106) [Link]

filtered_data = [y for y in map(f, data) if y is not None]

Corner cases and exception types

Posted Aug 25, 2019 22:43 UTC (Sun) by nevyn (guest, #33129) [Link]

So how would you rewrite the "filtered_data" example? It's plenty concise for me.
Which is:
    filtered_data = [y for x in data if (y := f(x)) is not None]
...the obvious simple choice is:
    # Just iterate data and return the value we want...
    filtered_data = [x for x in iter_f(data) if x is not None]
...which leads to the even better version:
    # Get a list of filtered values we want...
    filtered_data = list_filter_f(data)
...the original idea behind python wasn't to cram everything on a single line to win perl golf competitions.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds