
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to mask a list using values from another list
When it is required to mask a list with the help of values from another list, list comprehension is used.
Example
Below is a demonstration of the same
my_list = [5, 6, 1, 9, 11, 0, 4] print("The list is :") print(my_list) search_list = [2, 10, 6, 3, 9] result = [1 if element in search_list else 0 for element in my_list] print("The result is :") print(result)
Output
The list is : [5, 6, 1, 9, 11, 0, 4] The result is : [0, 1, 0, 1, 0, 0, 0]
Explanation
A list is defined and is displayed on the console.
Another list of elements is defined.
The list comprehension is used to iterate through the list and search for the element in the list.
The results are assigned to a variable.
This result is displayed on the console.
- Related Articles
- Python Program to print unique values from a list
- How can I sort one list by values from another list in Python?
- How to remove index list from another list in python?
- Java Program to copy value from one list to another list
- Update a list of tuples using another list in Python
- Python - Filter even values from a list
- C# program to print unique values from a list
- Java program to print unique values from a list
- Python - Insert list in another list
- Program to find folded list from a given linked list in Python
- Get unique values from a list in Python
- Python program to extract Keywords from a list
- Python | Sort the values of first list using second list
- Java Program to create a new list with values from existing list with Function Mapper
- Java Program to create a new list with values from existing list with Lambda Expressions

Advertisements