A
A
Andrew2022-01-04 00:42:17
Python
Andrew, 2022-01-04 00:42:17

How to use all/any with if?

The essence of the question is in the title. For example, I need to compare a given element with each element of a given list, and if it is equal to at least one of the list, perform some action. You can use if x in ['fd', 'tf', 33] but is it possible to use any() somehow here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-04
@Vindicar

if any(x==item for item in ['fd', 'tf', 33]):
But this only makes sense if the comparison is not trivial, and just in won't work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question