Online Tutorials & Training Materials | STechies.com
Register Login

How to check if an item exists in list in Python?

|| 0

How to check if an item exists in list in Python?
Stechies

In this article, we check out an item exists in the list or not with different operators.
We have this list of strings to use throughout in the examples and in the program. 

# List of string 
    liststrings = ['Hy' , 'how', 'at', 'this', 'where', 'from']

Using “in” operator 

If element exists in the list it will return true otherwise false.  

Example

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

Conditional check if element is not in List or not:

Example

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

Using list.count() function

list.count(elem)

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

Example:

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

Using any() function

any() function checks if any Element of the given list is True.

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

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

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

def Matchcheck(elem):
    if len(elem) == 4:
        return True;
    else :
        return False;

'''    
    Check for any string that satisfies the condition in checkIfMatch() function  exist in List
'''
result = any(Matchcheck for elem in liststrings)

Complete program

def Matchcheck(elem):
    if len(elem) == 5:
        return True;
    else :
        return False;
def main():
    # List of string 
    liststrings = ['Hy' , 'how', 'at', 'this', 'where', 'from']
    # Print the List
    print(liststrings)
    '''    
        check if element exist in list using 'in'
    '''
    if 'at' in liststrings :
        print("Yes, 'at' found in List : " , liststrings)
    '''    
        check if element NOT exist in list using 'in'
    '''
    if 'time' not in liststrings :
        print("Yes, 'time' NOT found in List : " , liststrings)    
    '''    
        check if element exist in list using count() function
    '''
    if liststrings.count('at') > 0 :
        print("Yes, 'at' found in List : " , liststrings)
    '''    
        check if element exist in list based on custom logic
        Check if any string with length 4 exist in List
    '''
    result = any(len(elem) == 4 for elem in liststrings)
    if result:
        print("Yes, string element with size 4 found")
    '''    
        Check if any string that satisfies the condition in checkIfMatch() function  exist in List
    '''
    result = any(Matchcheck for elem in liststrings)
    if result:
     print("Yes, string element with size 4 found")
    
if __name__ == '__main__':
    main()

Output of the program

['Hy', 'how', 'at', 'this', 'where', 'from']
("Yes, 'at' found in List : ", ['Hy', 'how', 'at', 'this', 'where', 'from'])
("Yes, 'time' NOT found in List : ", ['Hy', 'how', 'at', 'this', 'where', 'from'])
("Yes, 'at' found in List : ", ['Hy', 'how', 'at', 'this', 'where', 'from'])
Yes, string element with size 4 found
Yes, string element with size 4 found

 


Related Articles