Open In App

Python | Test if all elements are present in list

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


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: 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))


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

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


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: 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


Output

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.

 



Similar Reads

Python | Remove all values from a list present in other list
Sometimes we need to perform the operation of removing all the items from the lists that are present in another list, i.e we are given some of the invalid numbers in one list which need to be get ridden from the original list. Let's discuss various ways How to remove the elements of a list from another list in Python. Illustration: Input: List one
10 min read
Python - Test rear digit match in all list elements
Sometimes we may face a problem in which we need to find a list if it contains numbers ending with the same digits. This particular utility has an application in day-day programming. Let’s discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can approach this problem by converting the elements to t
6 min read
Python - Test if all elements in list are of same type
Sometimes, while working with Python, we can have a problem in which we need to test if all the elements of argument are of same type or not. This can have application in many domains such as data Science and day-day programming. Lets discuss certain ways in which this task can be performed. Method #1: Using loop + isinstance() The combination of a
6 min read
Python - Test for all Even elements in the List for the given Range
Given a List of elements, test if all elements are even in a range. Input : test_list = [3, 1, 4, 6, 8, 10, 1, 9], i, j = 2, 5 Output : True Explanation : 4, 6, 8, 10, all are even elements in range. Input : test_list = [3, 1, 4, 6, 87, 10, 1, 9], i, j = 2, 5 Output : False Explanation : All not even in Range. Method #1: Using loop In this, we iter
2 min read
Python program to test if all elements in list are maximum of K apart
Given a list of numbers, the task is to write a Python program to test if all elements are maximum of K apart. Examples: Input : test_list = [475, 503, 425, 520, 470, 500], K = 100Output : True Explanation : Maximum element is 520 and minimum is 425, 520-425 = 95, which is less than 100, hence elements are in range. Input : test_list = [475, 503, 4
5 min read
Python - Test if elements of list are in Min/Max range from other list
Given two lists, the task is to write a Python Program to return true if all elements from second list are in range of min and max values of the first list. Examples: Input : test_list = [5, 6, 3, 7, 8, 10, 9], range_list = [4, 7, 9, 6] Output : True Explanation : Min and max in list 1 are 3 and 10, all elements are in range in other list. Input :
6 min read
Sum of all Palindrome Numbers present in a Linked list
Given a linked list with integer node values, the task is to find the sum of all Palindrome Numbers present as Node values.Examples: Input: 13 -> 212 -> 22 -> 44 -> 4 -> 3 Output: 285 Explanation: The sum of palindrome numbers {22, 212, 44, 4, 3} is 285Input: 19 -> 22 -> 141 Output: 163 Approach: In order to solve this problem
8 min read
Python Program to print all distinct uncommon digits present in two given numbers
Given two positive integers A and B, the task is to print the distinct digits in descending order, which are not common in the two numbers. Examples: Input: A = 378212, B = 78124590Output: 9 5 4 3 0Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. The digits {1, 2, 6, 7} are common in both numbers. Inpu
5 min read
Python | Add only numeric values present in a list
Given a list containing characters and numbers, the task is to add only numbers from a list. Given below are a few methods to complete a given task. Method #1: Using filter() and lambda C/C++ Code # Python code to demonstrate # how to add only numbers present # in a list of characters and numbers # initialising lists ini_list = [1, 2, 3, 4, 'a', 'b
4 min read
Python - Find dictionary keys present in a Strings List
Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the extraction of dictionary keys from strings list feeded. This problem can have application in many domains including data. Lets discuss certain ways in which this task can be performed. Method #1: Using list comprehension This is brute force way in
7 min read
Practice Tags :