In this article we will discuss if a list contains all or any elements of another list.

Suppose we have have two list i.e.

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

# List of string
list2 = ['there' , 'hello', 'Hi']

Check if list1 contains all elements of list2 using all()

'''    
    check if list1 contains all elements in list2
'''
result =  all(elem in list1  for elem in list2)

if result:
    print("Yes, list1 contains all elements in list2")    
else :
    print("No, list1 does not contains all elements in list2"

Python all() function checks if all Elements of given Iterable is True. So, convert the list2 to Iterable and for each element in Iterable i.e. list2 check if element exists in list1.

Check if list1 contains any elements of list2 using any()

'''    
    check if list1 contains any elements of list2
'''
result =  any(elem in list1  for elem in list2)

if result:
    print("Yes, list1 contains any elements of list2")    
else :
    print("No, list1 contains any elements of list2")

Python any() function checks if any Element of given Iterable is True. So, convert the list2 to Iterable and for each element in Iterable i.e. list2 check if any element exists in list1.

Complete example is as follows,

def main():
    
    # List of string 
    list1 = ['Hi' ,  'hello', 'at', 'this', 'there', 'from']
    
    # List of string
    list2 = ['there' , 'hello', 'Hi']
    
    '''    
        check if list1 contains all elements in list2
    '''
    result =  all(elem in list1  for elem in list2)
    
    if result:
        print("Yes, list1 contains all elements in list2")    
    else :
        print("No, list1 does not contains all elements in list2")    
        
    
    '''    
        check if list1 contains any elements of list2
    '''
    result =  any(elem in list1  for elem in list2)
    
    if result:
        print("Yes, list1 contains any elements of list2")    
    else :
        print("No, list1 contains any elements of list2")        

        
if __name__ == '__main__':
    main()


Output:
Yes, list1 contains all elements in list2
Yes, list1 contains any elements of list2