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.
Edit / more clarification:
It seems what I would need is any( x.method() for x in list ) but also with a reference to x so that I can then use x if the condition was true.
Here's the whole concept I'm trying to get again:
if condition:
do this
elif list[0].method():
do this to list[0]
elif list[1].method():
do this to list[1]
...
elif list[n].method():
do this to list[n]
else:
do this
where method() is some method that returns True or False, and n is the size of the list and not a constant.
list? What issome_condition? What isdo this? What isx.method()? What isdo this to x? What isdo something else? Exactly what output do you expect to see?