How can I remove duplicates from a list without changing its order in Python?
Davide S
davide s profile pic

To remove duplicates from a list without changing its order in Python, you can use several approaches. Here's a detailed explanation of a few commonly used methods: Using a Set and List Comprehension: Sets are unordered collections that only contain unique elements. By converting the list to a set and then back to a list, you can eliminate duplicate elements while preserving the original order.

1
2
3
4
5
6

original_list = [3, 1, 2, 2, 1, 4, 3, 5]

unique_list = list(set(original_list))

print(unique_list)

In this example,set(original_list) converts theoriginal_list to a set, removing duplicate elements. Then,list() converts the set back to a list, ensuring that the original order is maintained. The resultingunique_list contains only the unique elements[3, 1, 2, 4, 5]. Using a Temporary List: You can iterate over the original list and add each element to a temporary list only if it hasn't been encountered before. This method preserves the order of the original list.

1
2
3
4
5
6
7
8
9

original_list = [3, 1, 2, 2, 1, 4, 3, 5]

unique_list = []
for item in original_list:
    if item not in unique_list:
   unique_list.append(item)

print(unique_list)

In this example, thefor loop iterates over each element in theoriginal_list. If the element is not already present in theunique_list, it is added usingunique_list.append(item). The resultingunique_list contains only the unique elements[3, 1, 2, 4, 5]. Using the OrderedDict class (Python 3.7+): In Python 3.7 and later versions, dictionaries preserve the insertion order of elements. You can utilize theOrderedDict class from thecollections module to remove duplicates while maintaining the order.

1
2
3
4
5
6
7
8

from collections import OrderedDict

original_list = [3, 1, 2, 2, 1, 4, 3, 5]

unique_list = list(OrderedDict.fromkeys(original_list))

print(unique_list)

In this example,OrderedDict.fromkeys(original_list) creates an ordered dictionary where the keys are the unique elements from theoriginal_list. Converting the keys back to a list usinglist() produces theunique_list with the desired order[3, 1, 2, 4, 5]. Summary: To remove duplicates from a list without changing its order in Python, you can use various methods. These include converting the list to a set and back to a list, iterating over the list and adding elements to a temporary list only if they haven't been encountered before, or utilizing theOrderedDict class to preserve order while removing duplicates. Choose the method that best suits your needs based on the specific requirements and context of your program.