How to check if one of the following items is in a list with Python?

Sometimes, we want to check if one of the following items is in a list with Python.

In this article, we’ll look at how to check if one of the following items is in a list with Python.

How to check if one of the following items is in a list with Python?

To check if one of the following items is in a list with Python, we can use the any function.

For instance, we write

a = [1,2,3,4]
b = [2,7]
any(x in a for x in b)

to check if any element in list b is also in a.

We call any with the the iterator that has the items in b that’s also in a with x in a for x in b to do the check.

Conclusion

To check if one of the following items is in a list with Python, we can use the any function.