
- 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 Extract Elements from a List in a Set
When it is required to extract elements from a List in a Set, a simple ‘for’ loop and a base condition can be used.
Example
Below is a demonstration of the same
my_list = [5, 7, 2, 7, 2, 4, 9, 8, 8] print("The list is :") print(my_list) search_set = {6, 2, 8} my_result = [] for element in my_list: if element in search_set: my_result.append(element) print("The result is :") print(my_result)
Output
The list is : [5, 7, 2, 7, 2, 4, 9, 8, 8] The result is : [2, 2, 8, 8]
Explanation
A list is defined and is displayed on the console.
Another set with specific elements is defined.
An empty list is defined.
The list is iterated over, and element is searched for in the ‘set’.
If it is found, it is added to the empty list.
This is the result that is displayed on the console.
- Related Articles
- Python program to extract Keywords from a list
- Python – Extract elements from Ranges in List
- Python program to extract characters in given range from a string list
- Python program to remove Duplicates elements from a List?
- Python Program to Remove Palindromic Elements from a List
- Python Program that extract words starting with Vowel From A list
- Python program to find N largest elements from a list
- Program to find duplicate item from a list of elements in Python
- Python program to remove duplicate elements from a Circular Linked List
- Python – Extract range of Consecutive similar elements ranges from string list
- Python program to extract Mono-digit elements
- Python Program to extracts elements from a list with digits in increasing order
- Python program to extract only the numbers from a list which have some specific digits
- Python – Extract element from a list succeeded by K
- Python program to remove duplicate elements from a Doubly Linked List\n

Advertisements