Search code examples
pythonlistcontains

Check if string in list contain all the elements from another list


I have two lists and I want to check if my list of strings contains all the elements of the second list.

I would like it to return True only if all the elements in list2 are in substrings of elements in list1 (True in this example).

Here is my code :

list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']
        
if all(elem in elem in list1.tolist() for elem in list2):

Solution

  • Try this:

    list1 = ['banana.two', 'apple.three', 'raspberry.six']
    list2 = ['two', 'three']
    
    
    def check(strings, substrings):
        for substring in substrings:
            if not (any(substring in string for string in strings)):
                return False
        return True
    
    
    print(check(list1, list2))