Skip to main content

How to remove duplicates from a list in Python

How to remove duplicates from a list in Python.

Here's a step-by-step tutorial on how to remove duplicates from a list in Python:

Step 1: Create a List

First, you need to create a list with some duplicate elements. Let's create a list called my_list with duplicate elements:

my_list = [1, 2, 3, 3, 4, 5, 5, 6]

Step 2: Use the set() Function

One of the simplest ways to remove duplicates from a list is by converting the list into a set. A set is an unordered collection of unique elements. We can utilize this property to remove duplicates from the list. Here's how you can do it:

my_list = list(set(my_list))

In the above code, we convert the list my_list into a set using the set() function. Then, we convert it back to a list using the list() function. This will remove all the duplicate elements from the list.

Step 3: Use a List Comprehension

Another way to remove duplicates from a list is by using a list comprehension. Here's how you can do it:

my_list = [x for i, x in enumerate(my_list) if x not in my_list[:i]]

In the above code, we iterate through the list using a list comprehension. For each element x at index i, we check if it is already present in the list before index i. If it is not present, we keep it in the new list, thereby removing duplicates.

Step 4: Use the filter() Function

You can also use the filter() function to remove duplicates from a list. Here's how you can do it:

my_list = list(filter(lambda x: my_list.count(x) == 1, my_list))

In the above code, we use a lambda function with the filter() function. We check if the count of each element in the list is equal to 1. If it is, we keep that element in the list, thereby removing duplicates.

Step 5: Use a Dictionary

You can also use a dictionary to remove duplicates from a list. Here's how you can do it:

my_list = list(dict.fromkeys(my_list))

In the above code, we convert the list my_list into a dictionary using the fromkeys() method. Since a dictionary only keeps unique keys, this step removes duplicates. Finally, we convert the dictionary back into a list.

Step 6: Preserve the Order

If you want to remove duplicates while preserving the order of elements in the list, you can use the dict.fromkeys() method with a list comprehension. Here's how you can do it:

my_list = list(dict.fromkeys(my_list).keys())

In the above code, we use the dict.fromkeys() method to remove duplicates. Then, we extract the keys of the resulting dictionary using the keys() method. Finally, we convert the keys into a list.

That's it! You have successfully removed duplicates from a list in Python using multiple methods. Choose the method that best suits your requirements.