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: [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: [‘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()


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 to remove duplicates from list in python programming language.

Leave a Reply

20 − 6 =