Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to mask a list using values from another list

Improve Article
Save Article
  • Last Updated : 28 Oct, 2022
Improve Article
Save Article

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]

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]

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

Output

The original list is : [5, 2, 1, 9, 8, 0, 4]
The Boolean Masked list : [0, 0, 1, 1, 1, 0, 0]

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

Output

The original list is : [5, 2, 1, 9, 8, 0, 4]
('The Boolean Masked list : ', [0, 0, 1, 1, 1, 0, 0])

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!