mardi 31 mars 2015

Python: loop in elif condition

I'm running Python 3.


Is it possible to put a loop in the condition for elif? Here's the basic idea I'm trying to achieve. The number of items in the list is not constant.



if some_condition:
do this
elif [ x.method() for x in list ]:
do this to x
else:
do something else


Now, this comes to my mind:



if some_condition:
do this
for x in list:
if x.method():
do this to x
break


But I'm trying to avoid running all the if statements, there's a lot of stuff going on in them. And I would like to get it in the elif part specifically and not in else.


Is there a way to achieve the first code example in Python without using e.g. boolean helper variables:



if some_condition:
do this
first_thing_happened = True
if not first_thing_happened:
for x in list:
if x.method():
do this to x
break

Aucun commentaire:

Enregistrer un commentaire