Python : Check if a list contains all the elements of another list

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.

Advertisements

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

 

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

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