Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *