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.
Frequently Asked:
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 :
Latest Python - Video Tutorial
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.
Latest Video Tutorials