How to check List Contains another list in Python

In this article, we are going to learn How to check List Contains another list in Python, which means we will check if a given list is fully present in another list.

Sometimes while data manipulation in python we have a list with complex data, and we have to check if any, all elements list contains is in another list. If we try doing this work manually, it is too much code and time. But if you know these techniques which we are going to learn in this tutorial then, this will make Your work simple with less code.

Here are a list of ways we are using to find check if any all elements list contains is in another list.

1. Using set intersection() method

in this example, we will find List Contains another list in Python by using the set intersection() method. The intersection() method is used to find the common elements between two sets.To find the elements common between the two lists,

  • we will first convert them into sets and then check common elements using intersection() method. This will be our first technique to do the job.
  • This method will involve conversion to sets and checking the validity.

Let us dive into example.

#Program List Contains another list in Python

animal_list = ['dog','cat','mouse','cat','bee','cat','cat','rat']
sub_animal_list = ['cat','rat','bee']
animal_set = set(animal_list)

if(animal_set.intersection(set(sub_animal_list))):
  print(' animal_list elements exist in sub_animal_list')
else:
    print('animal_list elements not exist in sub_animal_list')

Output

 animal_list elements exist in sub_animal_list

2. Using nested loop

The second technique that we are going to learn is by using nested for loops. This is a simple iteration-based approach. Here, in this code snippet, we are using for loops to check all the elements of the original list that exist in the sub-list by using the nesting loops. This approach is just verifying the elements one by one.

Let us dive into example

animal_list = ['dog','cat','mouse','cat','bee','cat','cat','rat']
sub_animal_list = ['mouse','cat','rat']

#checking all list element exist in another list
def check_item_exist(org_list,sub_list):
 for j in range(len(org_list)-len(sub_list)+1): 
     for k in range(len(sub_list)): 
        if org_list[j + k] != sub_list[k]: 
                 break               
        else: 
            return True
 return False
print(check_item_exist(animal_list,sub_animal_list))

Output

True

3. any() method to check any element

Next Technique that we are going to learn is by using the any() method. In this code snippet, we are using any() method.

It takes an iterable(tuple, list, dictionary) as an argument and returns true if any one of the elements of iterable is true for a condition or all elements of iterable are true for a condition, returns false if iterable is empty or any or all elements returns false for a condition.

let us understand with an example of how to use to find all elements or any element that exists in another list

animal_list = ['dog','cat','mouse','cat','bee','cat','cat','rat']
sub_animal_list =['dog','cat','mouse']


if(any(elem in animal_list  for elem in sub_animal_list)):
    print( 'animal_list elements exist in sub_animal_list')    
else :
    print(" animal_list elements not exist in sub_animal_list")

 

Output

 animal_list elements exist in sub_animal_list

4. Using all() method

Last technique that we are going to learn is by using the all() method. This is somewhat similar to any() method approach but little bit difference is there when it comes to empty lists. all() method check all element of an iterable(list,tuple,dictionary) for a condition and return true else it return false.If the iterable is empty then it return true though. This is one difference from any() function.

animal_list = ['dog','cat','mouse','cat','bee','cat','cat','rat']
sub_animal_list =['dog','cat','lizard']


if(all(elem in animal_list for elem in sub_animal_list)):

    print( 'animal_list elements exist in sub_animal_list')    
else :
    print(" animal_list elements not exist in sub_animal_list")

Output

The output is from the else block because all the element of sub-list are not exist in the original list.

 animal_list elements not exist in sub_animal_list

Conclusion

We hope you could How to check List Contains another list in Python. By using these above ways you can now make your code simple and cleaner.

Happy Learning!!