This tutorial will discuss how to remove duplicates from a list in Python & maintain order.
Suppose we have a list of integers and we want to remove the duplicate elements while maintaining the original order of the elements.
To achieve this, we can iterate over the list elements one by one and place each element into a set. Before adding an element to the set, we check if it already exists there. If it does, it indicates a duplicate entry; in that case, we won’t add it to a new list & set. Otherwise, we add the element to the new list & set. By the end of this iteration, the new list will contain only unique elements, and all duplicate entries will have been removed. Importantly, this approach maintains the original order of the elements.
We have created a separate function for this purpose i.e. removeDuplicates(). It accepts a list as an argument, removes duplicate entries, and maintains the order of the elements. We can call this function whenever we need to remove duplicates from a list.
Let’s see the complete example,
def removeDuplicates(listObj): # Create a Set to keep track of # unique elements seenItems = set() # List to hold unique items uniqueList = [] # Loop through the list for item in listObj: if item not in seenItems: seenItems.add(item) uniqueList.append(item) return uniqueList sampleList = [11, 22, 33, 11, 22, 55, 66, 77, 88, 66] print("Original List:", sampleList) sampleList = removeDuplicates(sampleList) print("List after removing duplicates:", sampleList)
Output
Original List: [11, 22, 33, 11, 22, 55, 66, 77, 88, 66] List after removing duplicates: [11, 22, 33, 55, 66, 77, 88]
Summary
Frequently Asked:
Today, we learned how to remove duplicates from a list in Python & maintain order.