Python - Remove Duplicates from a list And Keep The Order Last Updated : 17 Dec, 2024 Comments Improve Suggest changes Like Article Like Report While lists provide a convenient way to manage collections of data, duplicates within a list can sometimes pose challenges. In this article, we will explore different methods to remove duplicates from a Python list while preserving the original order.Using dict.fromkeys()dict.fromkeys() method creates a dictionary with keys from the list, automatically removing duplicates because dictionary keys are unique. The order is preserved where dictionaries maintain insertion order. Python a = [1, 2, 2, 3, 4, 4, 5] b = list(dict.fromkeys(a)) print(b) Output[1, 2, 3, 4, 5] Explanation:dict.fromkeys(lst) creates a dictionary with list elements as keys.Let's see some more methods to remove duplicates and keep the order in Python List.Table of ContentUsing a Set()Using a LoopUsing OrderedDictUsing a Set()This method iterates through the list, adding elements to a set to track duplicates and using list comprehension to create a new list that retains only unique elements. Python a = [1, 2, 2, 3, 4, 4, 5] seen = set() b = [x for x in a if not (x in seen or seen.add(x))] print(b) Output[1, 2, 3, 4, 5] Using a LoopThis is a simple method that involves iterating over the list and appending elements to a new list if they are not already present. Python a = [1, 2, 2, 3, 4, 4, 5] b = [] for x in a: if x not in b: b.append(x) print(b) Output[1, 2, 3, 4, 5] Explanation:Loop through the original list.Check if the element is already in unique_list.Append it only if it’s not present.Using OrderedDictBefore Python 3.7, dictionaries were not guaranteed to maintain order. To remove duplicates while preserving order in older versions, we could use collections.OrderedDict. Python from collections import OrderedDict a = [1, 2, 2, 3, 4, 4, 5] b = list(OrderedDict.fromkeys(a)) print(b) Output[1, 2, 3, 4, 5] Explanation:OrderedDict.fromkeys() ensures unique keys while maintaining order. Comment More infoAdvertise with us Next Article Python - Remove Duplicates from a list And Keep The Order H harshsingh123 Follow Improve Article Tags : Python Python Programs python-list Practice Tags : pythonpython-list Similar Reads Python - Remove Duplicates from a List Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets 2 min read Remove Duplicate Strings from a List in Python Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.Pythona = ["Learn", "Python", "With" 3 min read Python | Remove duplicates from nested list The task of removing duplicates many times in the recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let's discuss certain ways in which this task can be achieved. Method #1 : Using sorted() + set() Th 5 min read Python Program For Removing Duplicates From An Unsorted Linked List Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43. Recommended: 4 min read Python | Remove duplicates based on Kth element tuple list Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop Th 8 min read Like