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.
print elemas an example, but it's not relevant. Assume I want to print all of the non-empty strings inmy_listusing only one single conditional statement and only one pass through my_list without forming a new list that contains only the non-empty strings frommy_list.print '\n'.join(item for item in my_list if item != '').if not (elem=='')is equivalent toif elemassuming they're all strings.