-2

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!

user2390182
  • 72,016
  • 6
  • 67
  • 89

2 Answers2

3

You can do:

my_list = [3, 2, 1, 2, 4, 3]
unique_list = list(dict.fromkeys(my_list))
# [3, 2, 1, 4]

This works because dict maintains insertion order unlike set.
Docs:

user2390182
  • 72,016
  • 6
  • 67
  • 89
0

One more way:

from more_itertools import unique_everseen
my_list = [3, 2, 1, 2, 4, 3]
list(unique_everseen(my_list))

# [3, 2, 1, 4]
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44