Given two lists, the task is to write a python program that marks 1 for elements present in the other list else mark 0.
Input : test_list = [5, 2, 1, 9, 8, 0, 4], search_list = [1, 10, 8, 3, 9]
Output : [0, 0, 1, 1, 1, 0, 0]
Explanation : 1, 9, 8 are present in test_list at position 2, 3, 4 and are masked by 1. Rest are masked by 0.Input : test_list = [5, 2, 1, 19, 8, 0, 4], search_list = [1, 10, 8, 3, 9]
Output : [0, 0, 1, 0, 1, 0, 0]
Explanation : 1, 8 are present in test_list at position 2, 4 and are masked by 1. Rest are masked by 0.
Method #1: Using list comprehension
In this, we iterate through search list and in operator used to check composition using list comprehension and assign 1 for presence and 0 for absence.
Python3
# Python3 code to demonstrate working of # Boolean composition mask in list # Using list comprehension # initializing list test_list = [ 5 , 2 , 1 , 9 , 8 , 0 , 4 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing search list search_list = [ 1 , 10 , 8 , 3 , 9 ] # list comprehension iteration and in operator # checking composition res = [ 1 if ele in search_list else 0 for ele in test_list] # printing result print ( "The Boolean Masked list : " + str (res)) |
Output:
The original list is : [5, 2, 1, 9, 8, 0, 4] The Boolean Masked list : [0, 0, 1, 1, 1, 0, 0]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using set() + list comprehension
In this, duplicate elements are removed from the search list to reduce search space using set(). Rest all the operations are similar to the above method.
Python3
# Python3 code to demonstrate working of # Boolean composition mask in list # Using set() + list comprehension # initializing list test_list = [ 5 , 2 , 1 , 9 , 8 , 0 , 4 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing search list search_list = [ 1 , 10 , 8 , 3 , 9 ] # list comprehension iteration and in operator # checking composition # set() removes duplicates res = [ 1 if ele in set (search_list) else 0 for ele in test_list] # printing result print ( "The Boolean Masked list : " + str (res)) |
Output:
The original list is : [5, 2, 1, 9, 8, 0, 4] The Boolean Masked list : [0, 0, 1, 1, 1, 0, 0]
Time Complexity: O(n)
Space Complexity: O(n)
Method #3 : Using count() method
Python3
# Python3 code to demonstrate working of # Boolean composition mask in list # initializing list test_list = [ 5 , 2 , 1 , 9 , 8 , 0 , 4 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing search list search_list = [ 1 , 10 , 8 , 3 , 9 ] res = [] for i in test_list: if (search_list.count(i)> = 1 ): res.append( 1 ) else : res.append( 0 ) # printing result print ( "The Boolean Masked list : " + str (res)) |
The original list is : [5, 2, 1, 9, 8, 0, 4] The Boolean Masked list : [0, 0, 1, 1, 1, 0, 0]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using map() and lambda function
Python
# Python3 code to demonstrate working of # Boolean composition mask in list # initializing list test_list = [ 5 , 2 , 1 , 9 , 8 , 0 , 4 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing search list search_list = [ 1 , 10 , 8 , 3 , 9 ] res = map ( lambda x : 1 if x in search_list else 0 , test_list) # printing result print ( "The Boolean Masked list : " , list (res)) |
The original list is : [5, 2, 1, 9, 8, 0, 4] ('The Boolean Masked list : ', [0, 0, 1, 1, 1, 0, 0])
Time complexity: O(n*n), where n is the length of the test_list. The map() and lambda function takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required
# Method 4 : Using collections.Counter()
Python3
import collections # initializing list test_list = [ 5 , 2 , 1 , 9 , 8 , 0 , 4 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing search list search_list = [ 1 , 10 , 8 , 3 , 9 ] # Create a Counter from search_list counter = collections.Counter(search_list) # Iterate through test_list and check if it's present in counter as key res = [ 1 if counter.get(ele) else 0 for ele in test_list] # printing result print ( "The Boolean Masked list : " + str (res)) |
The original list is : [5, 2, 1, 9, 8, 0, 4] The Boolean Masked list : [0, 0, 1, 1, 1, 0, 0]
# Time Complexity : O(N), where N is the number of elements in test_list
# Auxiliary Space : O(M), where M is the number of elements in search_list