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)

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值