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