Summary: In this tutorial, we will learn to remove duplicates from a list in Python.

There are multiple ways to remove duplicates from a list in Python. Let’s discuss each of them.

Recommended: Python List

Method 1: Using Loop

old_list = [5, 7, 8, 8, 5, 1]
new_list = []

for element in old_list:
    if element not in new_list:
        new_list.append(element)
        
print("Old List: ",old_list)
print("New List: ",new_list)

Output

Old List: [5, 7, 8, 8, 5, 1]
New List: [5, 7, 8, 1]


In this method, we traverse the old list using for loop and check for each element whether it is present in the new list or not.

By doing so, we only add unique elements to the new list.

This is a naive method and is not so efficient.

Method 2: Using Set()

Recommended: Python Set

old_list = ['a', 'b', 'c', 'a', 'b']

new_list = list(set(old_list))
        
print("Old List: ",old_list)
print("New List: ",new_list)

Output

Old List: [‘a’, ‘b’, ‘c’, ‘a’, ‘b’]
New List: [‘c’, ‘a’, ‘b’]


In this method, we first construct a set from the old list using the set() constructor and then convert it back to the list using the list() constructor.

By doing so, the duplicates gets removed because sets in Python cannot have duplicates values.

This is the easiest method to remove duplicates from a list but the drawback is that the order of elements is not preserved.

Method 3: Using collections.OrderedDict.fromkeys()

from collections import OrderedDict

old_list = [7, 8, 5, 5, 3, 7]

new_list = list(OrderedDict.fromkeys(old_list))
        
print("Old List: ",old_list)
print("New List: ",new_list)

Output

Old List: [7, 8, 5, 5, 3, 7]
New List: [7, 8, 5, 3]


In this method, we first convert the list into a dictionary using OrderedDict.fromkeys() method and then we convert it back to list using list() constructor method.

OrderedDict.fromkeys() eliminates the duplicates from the list because the dictionary in Python cannot have duplicate keys.

This is the fastest method in python to remove duplicates values from a list.

In this tutorial, we learned how to remove duplicates from a list using the python programming language.

Leave a Reply

5 × 3 =