Python program to mask a list using values from another list

When working with data analysis or filtering tasks, you often need to create a binary mask from one list based on values present in another list. Python provides several approaches to accomplish this task efficiently.

What is List Masking?

List masking creates a binary list (containing 0s and 1s) where 1 indicates the element exists in the reference list and 0 indicates it doesn't. This technique is commonly used in data filtering and boolean indexing.

Using List Comprehension

The most Pythonic approach uses list comprehension to create a mask ?

my_list = [5, 6, 1, 9, 11, 0, 4]
print("Original list:")
print(my_list)

search_list = [2, 10, 6, 3, 9]
print("Search list:")
print(search_list)

# Create binary mask using list comprehension
mask = [1 if element in search_list else 0 for element in my_list]

print("Binary mask:")
print(mask)
Original list:
[5, 6, 1, 9, 11, 0, 4]
Search list:
[2, 10, 6, 3, 9]
Binary mask:
[0, 1, 0, 1, 0, 0, 0]

Using Set for Better Performance

For larger lists, converting the search list to a set improves lookup performance from O(n) to O(1) ?

my_list = [5, 6, 1, 9, 11, 0, 4]
search_list = [2, 10, 6, 3, 9]

# Convert to set for faster lookups
search_set = set(search_list)
mask = [1 if element in search_set else 0 for element in my_list]

print("Optimized mask:")
print(mask)
Optimized mask:
[0, 1, 0, 1, 0, 0, 0]

Using Boolean Values

You can also create a boolean mask instead of binary integers ?

my_list = [5, 6, 1, 9, 11, 0, 4]
search_list = [2, 10, 6, 3, 9]

# Create boolean mask
bool_mask = [element in search_list for element in my_list]

print("Boolean mask:")
print(bool_mask)

# Filter original list using boolean mask
filtered_items = [item for item, mask in zip(my_list, bool_mask) if mask]
print("Filtered items:")
print(filtered_items)
Boolean mask:
[False, True, False, True, False, False, False]
Filtered items:
[6, 9]

Comparison of Methods

Method Time Complexity Best For
List Comprehension O(n*m) Small search lists
Set-based Lookup O(n) Large search lists
Boolean Mask O(n*m) Direct filtering

Conclusion

List masking is essential for data filtering tasks. Use set-based lookups for better performance with large datasets, and boolean masks when you need to filter the original list directly.

Updated on: 2026-03-26T03:01:23+05:30

840 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements