How can I remove duplicates from a list while keeping the original order in Python?
Remove duplicates from a list while keeping the original order.
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
How can I remove duplicates from a list while keeping the original order in Python?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted May 22, 2025, 1:23 AM EDT
To remove duplicates from a list in Python while preserving the original order, you can use a list comprehension along with a set to track duplicates.
def remove_duplicates(lst): return [item for idx, item in enumerate(lst) if item not in lst[:idx]] # Example usage my_list = [1, 2, 2, 3, 4, 4, 5] unique_list = remove_duplicates(my_list) print(unique_list) # Output will be [1, 2, 3, 4, 5]