Check if a number exists in a list in Python

In this article, we will discuss different ways to check if a list contains a given number or not.

Table Of Contents

Method 1: Using in operator

We can directly use the in operator to check if a given number exist in list or not. For example, let check if number 12 exists in a list or not.

listOfNumbers = [11, 12, 13, 14, 16, 17]

num = 12

# Check if given number exists in the list
if num in listOfNumbers:
    print('Yes, Number exists in the list')
else:
    print('No, Number does not exists in the list')

Output :

Yes, Number exists in the list

Yes, number 12 exists in the list.

Advertisements

Method 2: Using any() function

Iterate over all the list elements, and for each element, check if it matches the given number or not. If any element matches the given number, then the conditional expression will evaluate to True, otherwise it will evaluate to False. Pass all the resultant values to the any() function, to check if any expression is evaluating to True or not.

The any() function in Python, accepts a sequence of boolean values, and returns True if any value in this boolean list evaluates to True. So, in this case if any() function returns True, it means list has a number that is equal to the given number.

Let’s see an example. Here, we will check if list contains the number 12 or not.

listOfNumbers = [11, 12, 13, 14, 16, 17]

num = 12

# Check if given number exists in the list
if any(True for elem in listOfNumbers if elem == num):
    print('Yes, Number exists in the list')
else:
    print('No, Number does not exists in the list')

Output :

Yes, Number exists in the list

Yes, number 12 exists in the list.

Method 3: Using filter() function

Filter the list using List comprehension, and select only those elements that matches the given number. If the length of this filtered list is more than 0, then it means that list has a one occurrence of given number. Let’s see an example. Here, we will check if list contains the number 12 or not.

listOfNumbers = [11, 12, 13, 14, 16, 17]

num = 12

filteredList = list(filter(lambda elem: num == elem, listOfNumbers))

# Check if given number exists in the list
if len(filteredList) > 0:
    print('Yes, Number exists in the list')
else:
    print('No, Number does not exists in the list')

Output :

Yes, Number exists in the list

Yes, number 12 exists in the list.

Method 4: Using list.count() function

List provides a function count(). It accepts an element as argument, and returns the occurrence count of that number. So, to check if list contains a number or not, just pass that to the count() function. If returns anything more than 0, then it means list contains that number. Let’s see an example. Here, we will check if list contains the number 12 or not.

listOfNumbers = [11, 12, 13, 14, 16, 17]

num = 12

# Check if given number exists in the list
if listOfNumbers.count(num) > 0:
    print('Yes, Number exists in the list')
else:
    print('No, Number does not exists in the list')

Output :

Yes, Number exists in the list

Yes, number 12 exists in the list.

Summary

We learned about different ways to check if list contains a number or not in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top