Sometimes, while working with Python list, we have a problem in which we need to check for a particular list of values and want to be sure if a target list contains all the given values. This has it’s application in web development domain when required some type of filtering. Let’s discuss a way in which this task can be performed.
Method : Using list comprehension + all() This task can be performed using the inbuilt functionality of all(). The all() can be fed with list comprehension logic to check if element of test list is present in target list and rest is done by all().
Python3
# Python3 code to demonstrate working of # Test if all elements are present in list # Using list comprehension + all() # initializing list target_list = [ 6 , 4 , 8 , 9 , 10 ] # initializing test list test_list = [ 4 , 6 , 9 ] # printing lists print ( "The target list : " + str (target_list)) print ( "The test list : " + str (test_list)) # Test if all elements are present in list # Using list comprehension + all() res = all (ele in target_list for ele in test_list) # Printing result print ( "Does every element of test_list is in target_list ? : " + str (res)) |
The target list : [6, 4, 8, 9, 10] The test list : [4, 6, 9] Does every element of test_list is in target_list ? : True
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. list comprehension + all() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2: Using Counter() function
Python3
# Python3 code to demonstrate working of # Test if all elements are present in list # initializing list from collections import Counter target_list = [ 6 , 4 , 8 , 9 , 10 ] # initializing test list test_list = [ 4 , 6 , 9 ] # printing lists print ( "The target list : " + str (target_list)) print ( "The test list : " + str (test_list)) # Test if all elements are present in list freq = Counter(target_list) res = True for i in test_list: if i not in freq.keys(): res = False # Printing result print ( "Does every element of test_list is in target_list ? : " + str (res)) |
The target list : [6, 4, 8, 9, 10] The test list : [4, 6, 9] Does every element of test_list is in target_list ? : True
Method 3: Using set().issubset()
In this method, we first convert the test_list into a set, then use the issubset() function which returns true if all elements of the set are present in the target list.
Python3
# Python3 code to demonstrate working of # Test if all elements are present in list # Using set().issubset() # initializing list target_list = [ 6 , 4 , 8 , 9 , 10 ] # initializing test list test_list = [ 4 , 6 , 9 ] # printing lists print ( "The target list : " + str (target_list)) print ( "The test list : " + str (test_list)) # Test if all elements are present in list res = set (test_list).issubset(target_list) # Printing result print ( "Does every element of test_list is in target_list ? : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The target list : [6, 4, 8, 9, 10] The test list : [4, 6, 9] Does every element of test_list is in target_list ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using list comprehesion+recursion
In this example, list1 contains the elements [1, 2, 3, 4, 5], and list2 contains the elements [2, 4, 6]. Since not all elements in list2 are present in list1, the function returns False.
step-by-step approach:
1.Define a function called test_elements_in_list that takes two lists as arguments.
2.Use a list comprehension to iterate over each element elem in list2, and check if it is present in list1 using the in keyword. This will create a list of booleans that indicate whether each element of list2 is present in list1.
3.Use the all() function to check if all elements in present_in_list1 are True. If all elements are True, return True from the function, indicating that all elements in list2 are present in list1.
4.If any element in present_in_list1 is False, return False from the function, indicating that not all elements in list2 are present in list1.
5.Create two lists list1 and list2, and call the test_elements_in_list function with these lists as arguments.
6.Print the result of the function call using the print() function.
Python3
def test_elements_in_list(list1, list2): # iterate over each element elem in list2, and check if it is present in list1 # using a list comprehension present_in_list1 = [elem in list1 for elem in list2] # check if all elements in present_in_list1 are True using the all function if all (present_in_list1): # if all elements in list2 are present in list1, return True return True else : # otherwise, return False return False list1 = [ 1 , 2 , 3 , 4 , 5 ] list2 = [ 2 , 4 , 6 ] print (test_elements_in_list(list1, list2)) # False |
False
time complexity: O(n * m)
space complexity: O(n)
Method 5: Using numpy:
Algorithm:
1.Define the two lists to compare
2.Print the target list and the test list
3.Use the numpy module to convert the test list to a numpy array, and then use the numpy method in1d() to create a Boolean array that indicates whether each element of the test list is present in the target list
4.Use the numpy method all() to check whether all elements of the result array are True
5.Print the result
Python3
import numpy as np # initializing list target_list = [ 6 , 4 , 8 , 9 , 10 ] test_list = [ 4 , 6 , 9 ] # printing lists print ( "The target list : " + str (target_list)) print ( "The test list : " + str (test_list)) result = np.in1d(test_list, target_list). all () # Printing result print ( "Does every element of test_list is in target_list ? : " + str (result)) #This code is contributed by Jyothi pinjala |
Output:
The target list : [6, 4, 8, 9, 10]
The test list : [4, 6, 9]
Does every element of test_list is in target_list ? : True
Time complexity:
The time complexity of the np.in1d() function is O(n log n), where n is the length of the longer of the two input lists. The all() function has a time complexity of O(n), where n is the length of the input Boolean array. Therefore, the time complexity of the overall algorithm is O(n log n), where n is the length of the longer of the two input lists.
Auxiliary Space:
The np.in1d() function creates a Boolean array of the same length as the first input list, which has a space complexity of O(n), where n is the length of the longer of the two input lists. The all() function has a space complexity of O(1), since it only needs to store the result Boolean value. Therefore, the space complexity of the overall algorithm is O(n), where n is the length of the longer of the two input lists.