Python list contains: How to check if an item exists in list?
Tags: python, programming, beginners, tutorial
Published: 2022-05-25 07:08:44

In Python, lists are important containers as they store all kinds of data types as a collection. It can contain up to 536,870,912 items in a 32-bit system. Sometimes it is difficult to know if the list contains a particular item. Python has various ways by which we can find out if the list contains the item.

### Table of contents

*   [Python list contains - Introduction](#python-lists-contain-introduction)
*   [Check if the Python list contains an element using in operator](#check-if-the-python-list-contains-an-element-using-in-operator)
*   [Using for loop to check if the list contains an element in Python](#using-for-loop-to-check-if-the-list-contains-an-element-in-python)
*   [Using any() to check if the list contains an element](#using-any-to-check-if-the-list-contains)
*   [count() to check if the list contains an element](#count-to-check-if-the-list-contains)
*   [Closing thoughts](#closing-thoughts)

<span id="section1"></span>  

## Python lists contain - Introduction

Checking if an element is present in a list is one of the basic list operations in Python and there are many different ways we can check that. In this tutorial, we will be covering some of the ways to check if the lists contain an element.

<span id="section2"></span>  

## Check if the Python list contains an element using in operator

The most convenient way to check whether the list contains the element is using the in operator. Without sorting the list in any particular order, it returns TRUE if the element is there, otherwise FALSE.

The below example shows how this is done by using 'in' in the if-else statement.

#### Input:

    list = [Adam, Dean, Harvey, Mick, John]

    if 'John' in list:
    	print (" 'John' is found in the list")
    else
        print (" 'John' is not found in the list") 

    if 'Ned' in list:
        print (" 'Ned' is found in the list")
    else
        print (" 'Ned' is not found in the list")

<span id="section3"></span>

#### Output:

    'John' is found in the list
    'Ned' is not found in the list

## Using for loop to check if the list contains an element in Python

Another simple method to check if the list contains the element is looping through it. As the name suggests, the loop will match each element of the list with the element that we are looking for one by one and will only stop if there's a match or there is no match at all. The below example illustrates this.

#### Input:

    list = [Adam, Dean, Harvey, Mick, John]

    for name in list:     
    if name == 'Adam':         
    	  print ("Found the element")

<span id="section4"></span>

#### Output:

    Found the element

## Using any() to check if the list contains

The function any() is a built-in approach that checks for a match in a string with a match of each list element.

The below example shows how the any() function works. We check if there are any common items in the string, 'Adam lives in New York', and the list mentioned in the first line.

#### Input:

    list = [Adam, Dean, Harvey, Mick, John]
    string = "Adam lives in New York"

    print ("The original list is: " + str(list))
    print ("The original string is: " + string)

    result = any(item in string for item in list)

    print ("Does the string contain 'Adam': " + str(result))

<span id="section5"></span>

#### Output:

    The original list is: [Adam, Dean, Harvey, Mick, John]
    The original string is: Adam lives in New York
    Does the string contain 'Adam': True

## count() to check if the list contains

Another built-in method in Python, count() returns the number of times the passed element occurs in the list. If the element is not there in the list then the count() will return 0\. If it returns a positive integer greater than 0, it means the list contains the element.

#### Input:

    list = [Adam, Dean, Harvey, Mick, John]

    result = list.count(Harvey)

    if result > 0:
        print("Harvey exists in the list")
    else:
        print("Harvey does not exist in the list")

<span id="section6"></span>

#### Output:

    Harvey exists in the list

## Closing thoughts

In this tutorial, we have used the ['in' operator](https://www.askpython.com/python/examples/in-and-not-in-operators-in-python#:~:text=Python%20%E2%80%9Cin%E2%80%9D%20operator,into%20either%20True%20or%20False%20.), for loop, any() and count() methods to check whether a particular item exists in the list or not. One can learn more about other concepts related to Python [here](https://flexiple.com/python/list-of-lists-python).