Extract Elements from list in set - Python
Last Updated :
20 Feb, 2025
We are given a list and our task is to extract unique elements from list in a set. For example: a = [1, 2, 3, 4, 5, 2, 3, 6] here we would only extract those elements from list which are unique hence resultant output would be {1,2,3,4,5,6}.
Using Set Conversion
In this method we convert the list into a set which automatically removes duplicate elements. As a result, the set contains only unique elements, and its length gives the count of distinct items.
Python
a = [1, 2, 3, 4, 5, 2, 3, 6]
# Convert the list to a set to remove duplicates and store unique elements
u = set(a)
print(f"Unique elements: {u}")
OutputUnique elements: {1, 2, 3, 4, 5, 6}
Explanation:
- List "a" is converted to a set using set(a) which removes any duplicate elements keeping only unique values.
- Resulting set "u" contains unique elements from original list and they are printed.
Using Loop and Set
Using a loop and a set we can iterate through list and add each element to set ensuring that only unique elements are stored set automatically handles duplicates and its size will give count of distinct elements.
Python
a = [1, 2, 3, 4, 5, 2, 3, 6]
u = set()
# Iterate through each element in the list
for element in a:
# Add the element to the set
u.add(element)
print(f"Unique elements: {u}")
OutputUnique elements: {1, 2, 3, 4, 5, 6}
Explanation:
- We initialize an empty set and iterate through the list adding each element to set which automatically eliminates any duplicates.
- Finally it prints unique elements in set which contains only distinct values from original list.
Using filter() with set
Using filter() with a set allows filtering elements from a list by applying a condition and set automatically ensures only unique elements are included in the result. The filtered elements are then stored in a set to remove duplicates.
Python
a = [1, 2, 3, 4, 5, 2, 3, 6]
# Use filter() to select elements greater than 3
u = set(filter(lambda x: x > 3, a))
print(f"Unique elements greater than 3: {u}")
OutputUnique elements greater than 3: {4, 5, 6}
Explanation:
- filter() function is used with a lambda to select elements from list a that are greater than 3 and result is passed to set() to ensure uniqueness.
- output is unique elements greater than 3 from original list.
Similar Reads
Python - Extract Elements from Ranges in List We are given a list and list containing tuples we need to extract element from ranges in tuples list. For example, n = [10, 20, 30, 40, 50, 60, 70, 80, 90] and r = [(1, 3), (5, 7)] (ranges) we need to extract elements so that output should be [[20, 30, 40], [60, 70, 80]].Using List ComprehensionList
3 min read
Extract Elements from a Python List When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index. Python uses zero-based indexing, meaning the first element is at index 0. Pythonx = [10, 20, 30, 40, 50] # Extracts the last element a = x[0] print(
2 min read
Python - Extract records if Kth elements not in List Given list of tuples, task is to extract all the tuples where Kth index elements are not present in argument list. Input : test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)], arg_list = [6, 8, 8], K = 1 Output : [(5, 3), (7, 4), (1, 3)] Explanation : All the elements which have either 6 or 8 at 1s
4 min read
Python - Test if any set element exists in List Given a set and list, the task is to write a python program to check if any set element exists in the list. Examples: Input : test_dict1 = test_set = {6, 4, 2, 7, 9, 1}, test_list = [6, 8, 10] Output : True Explanation : 6 occurs in list from set. Input : test_dict1 = test_set = {16, 4, 2, 7, 9, 1},
4 min read
How to get specific elements from list in python In this article, we will learn how to get specific item(s) from given list. There are multiple methods to achieve this. Most simple method is accessing list item by Index. Pythona = [1, 'geeks', 3, 'for', 5] # accessing specific list item with their indices item1 = a[1] item3 = a[2] item5 = a[4] # n
3 min read