0
0
PythonProgramBeginner · 2 min read

Python Program to Find Unique Elements Using Set

You can find unique elements in a list by converting it to a set using unique_elements = set(your_list), which removes duplicates and keeps only unique values.
📋

Examples

Input[1, 2, 2, 3, 4, 4, 5]
Output{1, 2, 3, 4, 5}
Input["apple", "banana", "apple", "orange"]
Output{'apple', 'banana', 'orange'}
Input[]
Outputset()
🧠

How to Think About It

To find unique elements, think of a set as a special container that only keeps one copy of each item. By putting all items from your list into a set, duplicates automatically disappear, leaving only unique elements.
📐

Algorithm

1
Get the input list of elements.
2
Convert the list into a set to remove duplicates.
3
Return or print the set of unique elements.
💻

Code

python
def find_unique_elements(lst):
    unique_elements = set(lst)
    return unique_elements

# Example usage
sample_list = [1, 2, 2, 3, 4, 4, 5]
print(find_unique_elements(sample_list))
Output
{1, 2, 3, 4, 5}
🔍

Dry Run

Let's trace the list [1, 2, 2, 3, 4, 4, 5] through the code

1

Input list

lst = [1, 2, 2, 3, 4, 4, 5]

2

Convert list to set

unique_elements = set([1, 2, 2, 3, 4, 4, 5]) results in {1, 2, 3, 4, 5}

3

Return unique elements

Return {1, 2, 3, 4, 5}

IterationCurrent ElementSet Contents
11{1}
22{1, 2}
32{1, 2}
43{1, 2, 3}
54{1, 2, 3, 4}
64{1, 2, 3, 4}
75{1, 2, 3, 4, 5}
💡

Why This Works

Step 1: Using a set removes duplicates

A set in Python automatically keeps only one copy of each element, so converting a list to a set removes repeated items.

Step 2: Conversion is simple and fast

Using set() is a built-in, efficient way to get unique elements without writing loops.

Step 3: Result is a set of unique elements

The output is a set containing only unique values from the original list.

🔄

Alternative Approaches

Using a loop and a new list
python
def find_unique_with_loop(lst):
    unique = []
    for item in lst:
        if item not in unique:
            unique.append(item)
    return unique

print(find_unique_with_loop([1, 2, 2, 3, 4, 4, 5]))
This keeps the order but is slower for large lists because it checks membership repeatedly.
Using dictionary keys
python
def find_unique_with_dict(lst):
    unique = list(dict.fromkeys(lst))
    return unique

print(find_unique_with_dict([1, 2, 2, 3, 4, 4, 5]))
This method keeps order and is faster than the loop method but uses more memory.

Complexity: O(n) time, O(n) space

Time Complexity

Converting a list to a set takes O(n) time because it processes each element once.

Space Complexity

The set stores unique elements, so in the worst case it uses O(n) space if all elements are unique.

Which Approach is Fastest?

Using set() is fastest for removing duplicates but does not preserve order; dictionary keys keep order but use more memory.

ApproachTimeSpaceBest For
Using set()O(n)O(n)Fast duplicate removal, order not needed
Loop with listO(n²)O(n)Preserving order, small lists
Dictionary keysO(n)O(n)Preserving order, faster than loop
💡
Use set() to quickly remove duplicates but remember sets do not keep order.
⚠️
Trying to use a set but expecting the original order of elements to be preserved.