Using Python to Check for Number in List

Today’s Python Tutorial brought to you one more essential topic that you should aware of ie., Checking the Number in a List using Python. You have an idea that List is an essential container in python as it stores elements of all the datatypes as a collection. There are different ways to check if a number or element is in a list or not. Explore more information about How to Check if element exists in list in Python by going through this tutorial.

In this Using Python to Check for Number in List Tutorial, you’ll learn:

Python List Contains

In order to check if the list contains a specific item in python, make use of inbuilt operators ie., ‘in’ or ‘not in’, or many other functions. One of the most convenient ways to check if an item exists on the list or not is the ‘in’ operator. Also, you can find some more approaches from the below modules along with the ‘in’ keyword approach.

Also Read: 

Check if element exists in list using python “in” Operator

Developers can check if a number or element in the list by the “in” keyword. In Python, we can use the ‘in’ keyword for lots of different purposes. In terms of today’s snippet, we’re going to use it to check if a particular number is in a list.

To begin, let’s say we have a list called coolNumbers. Take a look at the list below:

coolNumbers = [3, 1, 88, 7, 20, 95, 26, 7, 9, 34]

Now let’s say we want to be able to identify if certain numbers are in the list. For instance, maybe we want to know if 26 is in the list. Here’s how we would do it, using the ‘in’ keyword (make sure these lines of code appear in your Python files after you’ve already declared the list):

if 26 in coolNumbers:
   (print: "26 is a cool number")

That’s literally all it takes to check if a number is in a list using the ‘in’ keyword. It’s very natural. If the number is found in the list, the phrase “26 is a cool number” will be printed. But what if the number you’re looking for isn’t on your list? See the below module.

Check if Number exists in list in Python using “not in” Operator

Let’s say you’re looking for the number 29, and if it isn’t already on the list, you’d like to add it to the list, because, as everyone knows, 29 is a very cool number. You can actually use the “not in” keyword to check if a number is not in the list. Combined with an if statement, you can check if the number in question is not in your list, and you can make sure it gets added if that’s the case. Here’s how you would do it:

if 29 not in coolNumbers:
   coolList.append(29)

That’s all it takes. Not only is the process really simple, but, like a lot of Python code, it’s very intuitive. Try it out for yourself by making your own list of cool numbers (include all the numbers that you think are cool) and seeing if you can identify and add your own integers.

Check if element exists in list using list.count() function

By using thelist.count() method, you can easily check if the number or element exists in the Python list or not.

The syntax of the list.count() function is as follows:

list.count(elem)

It returns the occurrence count of the presented element in the list. If it’s greater than 0, it indicates a given item exists in the list. Check out the following code for understanding how it works:

""" Python code to demonstrate checking of element existence using List count() method """
 
# Initializing list
test_list = [10, 15, 20, 7, 46, 2808]
 
print("Checking if 15 exists in list")
 
# number of times element exists in list
exist_count = test_list.count(15)
 
# checking if it is more then 0
if exist_count > 0:
    print("Yes, 15 exists in list")
else:
    print("No, 15 does not exists in list")

Output: 

Yes, 15 exists in list

Check if element exist in list based on custom logic using any() function

The most traditional way to perform this task efficiently is using Python any() function. With the help of any() function, it checks for a match in a string with a match of each list element. Let’s make use of this method to check if any string element in the list is of length 5 i.e.,

'''    
    check if element exist in list based on custom logic
    Check if any string with length 5 exist in List
'''
result = any(len(elem) == 5 for elem in listOfStrings)
if result:
    print("Yes, string element with size 5 found")

Rather than this condition, we can use a separate function in ‘any’ function to match the condition i.e.,

def checkIfMatch(elem):
    if len(elem) == 5:
        return True;
    else :
        return False;
'''    
    Check if any string that satisfies the condition in checkIfMatch() function exists in the List
'''
result = any(checkIfMatch for elem in listOfStrings)

Python all() method to check if the list exists in another list

Here, you will understand to find if the python list contains all the items if another list and represent the result by python print() function.

We are taking two lists that had overlapping values. One of these is the big one that operates all the elements of the second one.

List1 – List1 contains all or some of the items of another list.
List2 – It is the subset of the first one.

Look at the below code:

# app.py

List1 = ['Homer',  'Bart', 'Lisa', 'Maggie', 'Lisa']

List2 = ['Bart', 'Homer', 'Lisa']

check = all(item in List1 for item in List2)

if check is True:
    print("The list {} contains all elements of the list {}".format(List1, List2))
else:
    print("No, List1 doesn't have all elements of the List2.")

Output:

The list ['Homer', 'Bart', 'Lisa', 'Maggie', 'Lisa'] contains all elements of the list ['Bart', 'Homer', 'Lisa']

Leave a Reply

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