A list of unique elements can be attained by converting a list to a dictionary or a set, then back to a list.
>>> original_list = [1,2,3,4,5,6,7,8,9,2,4,6,8]
>>> original_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 6, 8]
>>>
>>> unique_via_set = list(set(original_list))
>>> unique_via_dict = list(dict.fromkeys(original_list))
>>>
>>> unique_via_set
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> unique_via_dict
[1, 2, 3, 4, 5, 6, 7, 8, 9]
^Python 3.7.3
Main Question:
What is the difference between these two strategies?
Sub Questions:
- Is one more efficient than the other?
- Is there a use case that is more optimized for one or the other?
- Is there a practical difference in the run time between these two strategies?
Note: I'm not looking for the best or fastest way to get unique elements from a list, I am looking to compare the two methods above
Edit: Again, I am not looking for HOW to remove duplicates, I am merely looking for the practical differences in the methods above, if any