Method 3: Using the OrderedDict() Function
The third method we’ll be looking at is using the `OrderedDict()` function from the `collections` module. Here’s how to do it:
from collections import OrderedDict my_list = [1, 2, 2, 3, 4, 4, 5] new_list = list(OrderedDict.fromkeys(my_list)) print(new_list)
In the code above, we imported the `OrderedDict()` function from the `collections` module. We then created a new dictionary object from the list using the `fromkeys()` method, which automatically removes duplicates. Finally, we converted the dictionary back into a list using the `list()` function, and printed the new list without duplicates.
This method preserves the original order of the list, but can be slower than using a set for larger lists.