Remove Unordered Duplicate Elements from a List - Python
Last Updated :
18 Jan, 2025
Given a list of elements, the task is to remove all duplicate elements from the list while maintaining the original order of the elements.
For example, if the input is [1, 2, 2, 3, 1] the expected output is [1, 2, 3]. Let's explore various methods to achieve this in Python.
Using set
We can initialize an empty set to track the elements we've seen so far and create a new list to store the unique elements in their original order.
Python
# Initializing list of elements
a = [1, 2, 2, 3, 1]
# Initializing an empty set to track seen elements
s = set()
# Initializing an empty list to store unique elements
b = []
# Looping through the list
for x in a:
# If element is not in the set, add it to unique list and the set
if x not in s:
b.append(x)
s.add(x)
print(b)
Explanation:
- We initialize an empty set 's' to keep track of elements that have already been encountered.
- We loop through the list and only append elements to the unique list 'b' if they are not in the seen set 's'.
Let's explore some more methods and see how we can remove unordered duplicate elements from a list.
Using list comprehension with set
This method uses list comprehension along with a set for a more compact and precise solution.
Python
# Initializing list of elements
a = [1, 2, 2, 3, 1]
# Initializing an empty set to track seen elements
s = set()
# Using list comprehension to filter out duplicates
b = [x for x in a if not (x in s or s.add(x))]
print(b)
Explanation:
- We initialize the seen set 's' and use list comprehension to filter out duplicates.
- The expression x in s or s.add(x) ensures that elements are only added to the result if they haven't been seen before.
Using dict.fromkeys()
We can use dict.fromkeys() to remove duplicates while maintaining the order of elements.
Python
# Initializing list of elements
a = [1, 2, 2, 3, 1]
# Using dict.fromkeys() to remove duplicates
b = list(dict.fromkeys(a))
print(b)
Explanation:
- The dict.fromkeys() method creates a dictionary where the keys are the list elements. Since dictionaries do not allow duplicate keys, it automatically removes duplicates.
- We convert the dictionary back to a list to obtain the result.
Using collections.OrderedDict
We can use OrderedDict from the collections module to remove duplicates and maintain the order of elements.
Python
from collections import OrderedDict
# Initializing list of elements
a = [1, 2, 2, 3, 1]
# Using OrderedDict to remove duplicates while maintaining order
b = list(OrderedDict.fromkeys(a))
print(b)
Explanation:
- The OrderedDict.fromkeys() method removes duplicates while keeping the order of elements intact.
- It works similarly to dict.fromkeys() but preserves the order of insertion.
- This method is efficient and guarantees that the order is maintained.
Using for loop
This method uses a basic for loop to manually create a new list while checking if an element has already been added.
Python
# Initializing list of elements
a = [1, 2, 2, 3, 1]
# Initializing an empty list to store unique elements
b = []
# Looping through the list
for x in a:
# Adding element to unique list only if it's not already in it
if x not in b:
b.append(x)
print(b)
Explanation
- We initialize an empty list 'b' and loop through the original list.
- For each element, we check if it is already in the unique list 'b'. If not, we append it.
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
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
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 program to remove duplicate elements index from other list Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
7 min read
Python - Cross List Sync on duplicate elements removal Given two lists, perform syncing of all the elements of list with other list when removal of duplicate elements in first list. Input : test_list1 = [1, 1, 2, 2, 3, 3], test_list2 = [8, 3, 7, 5, 4, 1] Output : [1, 2, 3], [8, 7, 4] Explanation : After removal of duplicates (1, 2, 3), corresponding ele
9 min read