How do I remove duplicates from a list, while preserving order?
There are several ways to remove duplicates from a list while preserving order. One way is to convert the list to a set, and then convert it back to a list. The set will automatically remove duplicates, but since sets are unordered, you would need to convert it back to a list. Here's an example of this method:
original_list = [1, 2, 3, 4, 2, 5, 3, 6]
# Convert to set to remove duplicates
no_duplicates_set = set(original_list)
# Convert back to list
no_duplicates_list = list(no_duplicates_set)
print(no_duplicates_list)
# Output: [1, 2, 3, 4, 5, 6]
Watch a video course
Python - The Practical Guide
Another way is to use a list comprehension and check if an item is already in the new list before adding it. Here's an example:
original_list = [1, 2, 3, 4, 2, 5, 3, 6]
# Use list comprehension to add items to a new list if they haven't been added yet
no_duplicates_list = []
for item in original_list:
if item not in no_duplicates_list:
no_duplicates_list.append(item)
print(no_duplicates_list)
# Output: [1, 2, 3, 4, 5, 6]
Another way is to use the collections.OrderedDict() and then get the keys from the ordered dict. Here's an example:
from collections import OrderedDict
original_list = [1, 2, 3, 4, 2, 5, 3, 6]
no_duplicates_list = list(OrderedDict.fromkeys(original_list))
print(no_duplicates_list)
# Output: [1, 2, 3, 4, 5, 6]
You can choose the one which suits your need the best.