This post shows how to remove duplicate elements from a list in Python, while preserving the original order of elements.
Remove duplicates from a list in Python:
Explanation:
A simple way to deduplicate while keeping order is to walk through the list and add each element to a result list only if it hasn't been seen before, tracked using a set for fast lookups.
Python Program to Remove Duplicates from a List
numbers = [10, 20, 10, 30, 20, 40, 50, 30]
seen = set()
result = []
for num in numbers:
if num not in seen:
seen.add(num)
result.append(num)
print(f"List after removing duplicates: {result}")
Output:
List after removing duplicates: [10, 20, 30, 40, 50]
If order doesn't matter, list(set(numbers)) is a shorter way to do this — see more list-based problems in our Interview Prep section.
