Python Forum
do_somenthing_with(x) for x in my_list if is_valid(x)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
do_somenthing_with(x) for x in my_list if is_valid(x)
#1
It would be nice if python supported this kind of "expression comprehension", or do it?

I frecuently encounter situations in which I need to execute some function for a list of things, or a range of whatever, and wonder if it could be done in a one-liner like the message title. I know we have map's, and for's but I think this feels more natural and "pythonic"

Is there a syntax I'm not aware of? Or maybe someone proposed this in the past but coulnd't be done for any unwanted effects?
Reply
#2
_ = [do_something(obj) for obj in iterable if is_valid(obj)]
This is the same as it's in your topic.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
If you aren't trying to generate a list (or whatever sequence) then it is absolutely NOT more pythonic to use a comprehension. Using a comprehension for a side effect is an anti-pattern.

For example, the following is an example of what NOT to do:
[print(x) for x in range(10)]
You aren't actually interested in the list you create, and are abusing comprehension syntax (which is completely unnecessary).

So, do you actually want the list or do you just want a function to run several times with different inputs?
Reply
#4
Obviously, the point here isn't to make a list (which is trivial), but to be able to execute commands in a "comprehension fashion".

See the title.

Thanks
Reply
#5
But that is what's happening. It's exactly the same as map/filter, if you consume all of the input at once:
>>> def do_something_with(x):
...   print(x**2)
...
>>> def is_valid(x):
...   return x % 2 == 0
...
>>> things = range(10)
>>> [do_something_with(x) for x in things if is_valid(x)]
0
4
16
36
64
[None, None, None, None, None]
>>> list(map(do_something_with, filter(is_valid, things)))
0
4
16
36
64
[None, None, None, None, None]
Reply
#6
(Sep-24-2018, 07:05 AM)pystag Wrote: Obviously, the point here isn't to make a list (which is trivial), but to be able to execute commands in a "comprehension fashion".
And that is exactly my point. You shouldn't be using a comprehension. Yes, you can write a comprehension that does exactly what you want, but it isn't encouraged. If you care about being "pythonic" write a standard for loop and use multiple lines. If you don't care, knock-yourself out and shove it all into a one line list comp.
Reply
#7
(Sep-24-2018, 07:12 AM)nilamo Wrote: But that is what's happening.
He can do it in the way as my prev. post but I don't see any benefits. Readability of the code means something.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Sep-24-2018, 07:33 AM)Mekire Wrote: You shouldn't be using a comprehension. Yes, you can write a comprehension that does exactly what you want, but it isn't encouraged. If you care about being "pythonic" write a standard for loop and use multiple lines. If you don't care, knock-yourself out and shove it all into a one line list comp.

I agree I shouldn't be using a comprehension with the current python syntax. But wouldn't be nice if python itself implemented it? That's exactly the point of my post Wink Did someone ever make this suggestion to dev team?

inc(x) for x in range(5)
process(apply) for apply in my_folder
fix_hunger(country) for country in world if country is not in fed_countries
...etc...
Reply
#9
I really don't understand. You're saying you don't want a list comprehension, but what you're describing is a list comprehension, and all of your examples are legal syntax for list comprehensions or generator expressions. In what way is your suggestion different from what we already have?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020