Question
How to remove duplicates from a list?
Solution
✔ Verified
Method 1: list(set(my_list))
Removes duplicates, but order is not preserved.
my_list = [1, 2, 2, 3, 1]
unique_list = list(set(my_list))
print(unique_list) # Output: [1, 2, 3] (order may change)Method 2: list(dict.fromkeys(my_list))
Removes duplicates and keeps original order.
my_list = [1, 2, 2, 3, 1]
unique_list = list(dict.fromkeys(my_list))
print(unique_list) # Output: [1, 2, 3]Click here to download practice questions on
Python Lists