I have a list in Python that may contain duplicate elements, and I want to extract only the unique elements from it. I tried using the set() function to convert the list to a set, as I know that sets automatically remove duplicates. However, when I convert the list to a set and then back to a list, the order of the elements changes.
Here's the code I used:
python Copy code
my_list = [3, 2, 1, 2, 4, 3]
unique_list = list(set(my_list))
print(unique_list)
Expected Output:
[3, 2, 1, 4]
Actual Output:
[1, 2, 3, 4]
I expected the unique elements to be extracted while preserving the original order of the list. Is there a more efficient way to achieve this without altering the order of the elements? Thank you for your help!