Given a list that may contain repeated values, the task is to remove duplicate elements and keep only unique ones. For Example: Input: [1, 2, 2, 3, 4, 4, 5], Output: [1, 2, 3, 4, 5].
Let’s explore the different methods to remove duplicates from a list.
Using Dictionary fromkeys()
This method removes duplicates by converting list elements into dictionary keys using fromkeys(). Since keys must be unique, repeated values are discarded while the original order is maintained.
a = [1, 2, 2, 3, 4, 4, 5]
a = list(dict.fromkeys(a))
print(a)
Output
[1, 2, 3, 4, 5]
Explanation:
- dict.fromkeys(a) creates a dictionary with list values as keys
- Duplicate keys are ignored automatically
- list() converts the dictionary keys back into a list
Using set()
This method removes duplicates by converting the list into a set, which only stores unique values.
a = [1, 2, 2, 3, 4, 4, 5]
a = list(set(a))
print(a)
Output
[1, 2, 3, 4, 5]
Explanation:
- set() keeps only unique values
- The order of elements may change because sets are unordered
- list() converts the set back into a list
Using a Loop
This method checks each element and adds it to a new list only if it is not already present using for loop.
a = [1, 2, 2, 3, 4, 4, 5]
res = []
for x in a:
if x not in res:
res.append(x)
print(res)
Output
[1, 2, 3, 4, 5]
Explanation:
- for loop checks each element one by one
- if x not in res avoids adding duplicates
- append() adds only unique elements to the result list
Using List Comprehension
This method performs the same logic as the loop but uses list comprehension to add only unique elements.
b = [1, 2, 2, 3, 4, 4, 5]
res = []
[res.append(x) for x in b if x not in res]
print(res)
Output
[1, 2, 3, 4, 5]
Explanation:
- if x not in res filters duplicate values
- append() adds unique elements to the list
- List comprehension performs the operation in a compact form