Is it possible to basically do the following in Python:
for elem in my_list if elem:
#Do something with elem...
Note that I want to specifically avoid using map, lambdas, or filter to create a second list that gives the Boolean condition, and I don’t want to do the following:
for elem in [item for item in my_list if item]:
#Do something...
The latter method requires the construction of the Boolean list too. In my code, my_list can be very, very large.
Basically, the simplest way would be to write
for elem in my_list:
if elem:
#Do stuff...
but I specifically want this all in one line. If all-in-one-line won’t make the code actually any different than this last example I gave, that’s fine too and I will go with that.
You can use a generator expression instead of a list comprehension.