Python : How to Check if an item exists in list ? | python list contains

In this best python list contains article i will step by step discuss different methods to check if a given all the strings data element exists in list or not.

Here i learn to check if element in list python, iterate through list python, python check for duplicates in list, python find index of item in list, check if something is in a list python and how to check if something is in a list python.

python if in list,list contains python,python array contains,python list contains string,check if element in list python,iterate through list python,python find index of item in list,python check for duplicates in list,check if something is in a list python,how to check if something is in a list python,loop through list python
python list contains

Also Read: python skip lines starting with #

Check if element exists in list in Python

here in Python, List is an important contains in python as if save or stores ome elements of all the available datatypes as a one type of the collection.

Example 1 : Naive Method

products_ids = [ 1, 6, 3, 5, 3, 4 ] 
  
print("Checking if 4 exists in list ( using loop ) : ") 
  

for i in products_ids: 
    if(i == 4) : 
        print ("YEs Element Exists") 
  
print("Checking if 4 exists in list ( using in ) : ") 
  
 
if (4 in products_ids): 
    print ("YEs Element Exists") 

Also Read: How to Count Number of Lines in File with Python?

Example 2 : Using “in” Operator

Example i have a list of strings i.e.

# List of Message string 
customlistOfMessageTag = ['Mr' , 'thank', 'on', 'come', 'great', 'love']

Now let’s check if given list contains a Message string element ‘on’ ,

Read Also:  convert string to array python

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

Condition to check if element is in List :

elem in LIST

It will return True, if element exists in list else return false.
For example check if ‘on’ exists in list i.e.

'''    
    check if element exist in list using 'in'
'''
if 'on' in customlistOfMessageTag :
    print("Yes, 'on' found in List : " , customlistOfMessageTag)

here simple Condition to check if element is not in List :

'''    
    check if element NOT exist in list using 'in'
'''
if 'time' not in customlistOfMessageTag :
    print("Yes, 'time' NOT found in List : " , customlistOfMessageTag)

Also Read: convert string to array python

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

list.count(elem)

count(element) function returns the occurrence count of given element in the list. If its greater than 0, it means given element exists in list.

'''    
    check if element exist in list using count() function
'''
if customlistOfMessageTag.count('on') > 0 :
    print("Yes, 'on' found in List : " , customlistOfMessageTag)

Check if element exist in list based on custom logic

Python any() function checks if any Element of given Iterable is True.

Let’s use it to check if any Message string element in list is of length 5 i.e.

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

Instead of condition we can use separate function in any to match the condition i.e.

Read Also:  Python armstrong Number Program

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

Also Read: How to Use Time Sleep in Python Script?

python Complete example

Complete example is as follows

def checkIfMatch(elem):
    if len(elem) == 5:
        return True;
    else :
        return False;
def main():
    
    # List of Message string 
    customlistOfMessageTag = ['Mr' , 'thank', 'on', 'come', 'great', 'love']
    
    # Print the List
    print(customlistOfMessageTag)
    
    '''    
        check if element exist in list using 'in'
    '''
    if 'on' in customlistOfMessageTag :
        print("Yes, 'on' found in List : " , customlistOfMessageTag)
        
    '''    
        check if element NOT exist in list using 'in'
    '''
    if 'time' not in customlistOfMessageTag :
        print("Yes, 'time' NOT found in List : " , customlistOfMessageTag)    
    
    '''    
        check if element exist in list using count() function
    '''
    if customlistOfMessageTag.count('on') > 0 :
        print("Yes, 'on' found in List : " , customlistOfMessageTag)
    
    '''    
        check if element exist in list based on custom logic
        Check if any Message string with length 5 exist in List
    '''
    result = any(len(elem) == 5 for elem in customlistOfMessageTag)
    
    if result:
        print("Yes, Message string element with size 5 found")
    
    '''    
        Check if any Message string that satisfies the condition in checkIfMatch() function  exist in List
    '''
    result = any(checkIfMatch for elem in customlistOfMessageTag)
    
    if result:
        print("Yes, Message string element with size 5 found")
    
        
if __name__ == '__main__':
    main()

Output:

['Mr', 'thank', 'on', 'come', 'great', 'love']
Yes, 'on' found in List :  ['Mr', 'thank', 'on', 'come', 'great', 'love']
Yes, 'time' NOT found in List :  ['Mr', 'thank', 'on', 'come', 'great', 'love']
Yes, 'on' found in List :  ['Mr', 'thank', 'on', 'come', 'great', 'love']
Yes, Message string element with size 5 found
Yes, Message string element with size 5 found