Method 4: Using List Comprehension
The fourth and final method we’ll be looking at is using list comprehension to remove duplicates. Here’s how to do it:
my_list = [1, 2, 2, 3, 4, 4, 5] new_list = [] [new_list.append(item) for item in my_list if item not in new_list]
In the code above, we used list comprehension to loop through the original list (`my_list`) and append each item to `new_list` if it wasn’t already in the list. We then printed the new list without duplicates.
This method is concise and easy to read, but can be slower than using a set or dictionary for larger lists.