Python : How to Check if an item exists in list ? | Search by Value or Condition

In this article we will discuss different ways to check if a given element exists in list or not.

Suppose we have a list of strings i.e.

# List of string 
listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from']

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

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 ‘at’ exists in list i.e.

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

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

Advertisements

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 listOfStrings.count('at') > 0 :
    print("Yes, 'at' found in List : " , listOfStrings)

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 string element in 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")

Instead of condition we can use separate function in any 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  exist in List
'''
result = any(checkIfMatch for elem in listOfStrings)

Complete example is as follows,
def checkIfMatch(elem):
    if len(elem) == 5:
        return True;
    else :
        return False;


def main():
    
    # List of string 
    listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from']
    
    # Print the List
    print(listOfStrings)
    
    '''    
        check if element exist in list using 'in'
    '''
    if 'at' in listOfStrings :
        print("Yes, 'at' found in List : " , listOfStrings)
        
    '''    
        check if element NOT exist in list using 'in'
    '''
    if 'time' not in listOfStrings :
        print("Yes, 'time' NOT found in List : " , listOfStrings)    
    
    '''    
        check if element exist in list using count() function
    '''
    if listOfStrings.count('at') > 0 :
        print("Yes, 'at' found in List : " , listOfStrings)
    
    '''    
        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")
    
    '''    
        Check if any string that satisfies the condition in checkIfMatch() function  exist in List
    '''
    result = any(checkIfMatch for elem in listOfStrings)
    
    if result:
        print("Yes, string element with size 5 found")
    
        
if __name__ == '__main__':
    main()

Output:
['Hi', 'hello', 'at', 'this', 'there', 'from']
Yes, 'at' found in List :  ['Hi', 'hello', 'at', 'this', 'there', 'from']
Yes, 'time' NOT found in List :  ['Hi', 'hello', 'at', 'this', 'there', 'from']
Yes, 'at' found in List :  ['Hi', 'hello', 'at', 'this', 'there', 'from']
Yes, string element with size 5 found
Yes, string element with size 5 found

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

2 thoughts on “Python : How to Check if an item exists in list ? | Search by Value or Condition”

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top