Algorithm
- Create an empty list called
unique_list
to store unique elements. - Iterate through each element,
element
, in the input list.- Check if the
element
is not already inunique_list
.- If true, add the
element
tounique_list
.
- If true, add the
- Check if the
- After iterating through all elements in the input list, return the
unique_list
. - The returned
unique_list
contains only the unique elements from the input list.
Code Examples
#1 Code Example- Python Program Using set()
Code -
Python Programming
list_1 = [1, 2, 1, 4, 6]
print(list(set(list_1)))
Copy The Code &
Try With Live Editor
Output
[1, 2, 4, 6]
#2 Code Example- Python Program Remove the items that are duplicated in two lists
Code -
Python Programming
list_1 = [1, 2, 1, 4, 6]
list_2 = [7, 8, 2, 1]
print(list(set(list_1) ^ set(list_2)))
Copy The Code &
Try With Live Editor
Output
[4, 6, 7, 8]
Demonstration
Python Programing Example to Remove Duplicate Element From a List-DevsEnv