Does an item exists in a Python list?

3 Ways to Check If Element Exists in List Using Python

Do you want to know how to check if an item exists in a Python list? This tutorial will show you a Python operator you can use for that.

Verifying if an item is in a list is a very common check required as part of any Python application. To check if an item exists in a Python list you can use the “in operator”. This operator returns True if the item exists and False if the item doesn’t exist in the list. An alternative is to use the list count() method.

The terms item and element of a list used in this article have the same meaning.

Let’s go through some examples that will show you how to apply these concepts and grow your Python programming knowledge!

How Do You Check if an Item is in a List in Python?

The Python “in operator” allows you to find out if an item is in a list or not. It returns True if the item is in the list and False if the item is not present in the list.

Do you want to see how the “in” operator works?

Let’s use the Python shell to do that.

>>> fruits = ['orange', 'apple', 'banana', 'orange']
>>> 'orange' in fruits
True
>>> 'pineapple' in fruits
False

The “in operator” returns True when we check if the string ‘orange’ is in the list because that string exists in the list.

In the same way the “in operator” returns False when we check if the ‘pineapple’ string is in the list considering that this string doesn’t appear in the list.

Makes sense?

The values True and False are booleans. I have another step-by-step tutorial if you want to learn more about Python booleans.

How Do You Check if an Item is Not in a List Using Python?

The approach you can use to check if an element is not present in a list using Python is the opposite of the one we have seen in the previous section.

To check if an item is not in a list using Python you can use the “not in” operator that executes the opposite check compared to the “in” operator. The “not in” operator returns True if an item is not in the list and returns False if the item exists in the list.

Let’s see how this works using the Python shell.

>>> fruits = ['orange', 'apple', 'banana', 'orange']
>>> 'orange' not in fruits
False
>>> 'pineapple' not in fruits
True

As you can see in this example, when we check if the string ‘orange’ is not in the list, we get back a False boolean. That’s because the string ‘orange’ element exists in the list.

In the same way, when we check if the string ‘pineapple’ is not in the list, Python returns a True boolean. That’s because the string ‘pineapple’ is not in the list.

How to Check If a Python List Contains an Element Using an If Statement

Now that we have seen how the “in operator” works, you can use it to verify if an item or element is in a list in Python. We will see how you can use this knowledge in your Python programs.

In a Python program, you might have to implement different logic depending on the outcome of an if condition.

Let me tell you more…

Imagine that an if condition verifies if a given item is in a list or not and based on that your program has to take different actions.

The list we will use in the following example represents an inventory of products. The if statement checks if a new product we want to add to the inventory is already present in the inventory or not.

Here is what the Python code to do that would look like:

products = ['chair', 'table', 'lamp']
new_product = 'coffee mug'

if new_product in products:
    print(f"The {new_product} is already in the inventory.")
else:
    print(f"The {new_product} is not in the inventory and it will be added to it.")

First of all, we create a list called products that contains the current inventory of products.

Then we assign the value ‘coffee mug’ to the string variable new_product. This is just an example, in a real-world program this value would come from the user, for example by using the Python input() function.

The next step is to use an if statement to verify if the new_product is already in the list of existing products. To do that we are using the “in operator” explained in the previous sections.

You then print a message that changes depending on the fact that new_product is in the products list or not. The print statements in the example use Python F-strings.

You can further enhance the code by adding the new_product to the products list if that element is not in the list.

Here is how the body of the if statement changes:

if new_product in products:
    print(f"The {new_product} is already in the inventory.")
else:
    print(f"The {new_product} is not in the inventory and it will be added to it.")
    products.append(new_product)
    print(f"The updated inventory is {products}")

If the new_product is not in the list of products you use the list append() method to add it to the list. Then you print the updated list of products.

Using the List count() Method to Check If an Element Exists in a Python List

The list count() method can be used to check if an element is in a Python list because the count() method returns the number of times a given element appears in a list. An element is in a list if the value returned by the count() method is greater than 0.

Let’s start with an example using the Python shell:

>>> fruits = ['orange', 'apple', 'banana', 'orange']
>>> fruits.count('orange')
2
>>> fruits.count('pineapple')
0

As you can see in the example above…

One difference between using the “in operator” and using the count() method is that the latter can also tell if an element appears more than once in the list.

Here is how you can use the list count() method as part of an if / else statement:

products = ['chair', 'table', 'lamp']
new_product = 'coffee mug'

if products.count(new_product) > 0:
    print(f"The {new_product} is already in the inventory.")
else:
    print(f"The {new_product} is not in the inventory and it will be added to it.")
    products.append(new_product)
    print(f"The updated inventory is {products}")

The count() method can be useful because it tells how many times an item appears in a list.

But there is something else to consider…

The count() method is less efficient than the “in operator”. That’s because the “in operator” stops searching as soon as it finds the item while the count() method scans the entire list to count occurrences. This can become time-intensive for large lists.

Conclusion

Technically you could also use a Python for loop to check if an item is in a list by iterating through the list. At the same time, this doesn’t really make sense considering that Python provides the “in operator”.

Why would you make your code more complex when you can do the same check with a single line of code using the “in operator”?

In this tutorial, we have covered everything you need to know to be able to check if an element exists in a Python list. Now you know how to do this check whenever it’s required in your applications.

Depending on your requirements you will use the “in operator” or the list count() method. For example, if you are not interested in the number of occurrences of an item in a list the “in operator” is all you need.

Related article: In this article, I have explained the list count() method. Python lists provide more methods and the main ones are explained in the following Codefather tutorial about Python list methods.