How to Remove Duplicates from a list in Python

How to Remove Duplicates from a list in Python

When an element occurs more than once in a list, we refer to it as a duplicate.

In this tutorial, we will learn different methods of removing these duplicates from a list in Python.

  1. The Basic Approach
  2. Using List Comprehension
  3. Using Set()
  4. Using enumerate()
  5. Using OrderedDict

Let us discuss each one of them in detail.

The Basic Approach

In the first method, we will discuss the basic approach of removing duplicates from the list using Python.

Output:

The initialized list is  [12, 15, 11, 12, 8, 15, 3, 3]
The resultant list after removing duplicates is  [12, 15, 11, 8, 3]

Explanation

So, now it’s time to have a glance at the explanation of the above program.

  1. The first step is to initialize the list that contains duplicate values.
  2. After this, we have printed this list so that it becomes easy for us to compare the lists in the output.
  3. In the next step, we have declared an empty list that will hold all the unique values and then used for loop followed by decision making to check whether a particular element exists in the list or not, if it doesn’t exist, we will append it in the list.
  4. Finally, we have displayed the resultant list.

In the second program, we will make use of list comprehension to meet our objective.

Using List Comprehension

The following program illustrates the same-

Output:

The initialized list is  [12, 15, 11, 12, 8, 15, 3, 3]
The resultant list after removing duplicates is  [12, 15, 11, 8, 3]

Explanation

Let’s understand what we have done in the above program-

  1. The first step is to initialize the list that contains duplicate values.
  2. After this, we have printed this list so that it becomes easy for us to compare the lists in the output.
  3. In the next step, we have used list comprehension to build the same logic.
  4. Finally, we have displayed the resultant list.

In the third approach, we will see how set() can be used to obtain a list of distinct elements.

Using Set()

The program given below shows how it can be done-

Output:

The initialized list is  [12, 15, 11, 12, 8, 15, 3, 3]
The resultant list after removing duplicates is  [12, 15, 11, 8, 3]

Explanation

Let’s try and understand what happened here,

  1. The first step is to initialize the list that contains duplicate values.
  2. After this, we have printed this list so that it becomes easy for us to compare the lists in the output.
  3. In the next step, we have used set() inside list() and passed list_value1 as a parameter so that we can obtain a list of distinct elements.
  4. Finally, we have displayed the resultant list.

Using enumerate()

Now let’s see how enumerate() can be used to meet our objective.

The following program illustrates the same-

Output:

The initialized list is  [12, 15, 11, 12, 8, 15, 3, 3]
The resultant list after removing duplicates is  [12, 15, 11, 8, 3]

Explanation

  1. The first step is to initialize the list that contains duplicate values.
  2. After this, we have printed this list so that it becomes easy for us to compare the lists in the output.
  3. In the next step, we have used enumerate() inside list comprehension that will return a list containing unique elements.
  4. Finally, we have displayed the resultant list.

Finally, in the last program we will see how OrderedDict can be used to remove duplicates from list using Python.

Using OrderedDict

The program given below shows how it can be done-

Output:

The initialized list is  [12, 15, 11, 12, 8, 15, 3, 3]
The resultant list after removing duplicates is  [12, 15, 11, 8, 3]

Explanation

  1. The first step is to import OrderedDict from collections so that we can use it to remove duplicates from the list.
  2. In the next step, we have initialized the list that contains duplicate values.
  3. After this, we have printed this list so that it becomes easy for us to compare the lists in the output.
  4. Now we have passed list_values1 inside OrderedDict.fromkeys().
  5. Finally, we have displayed the resultant list that does not contain any duplicate elements.

Conclusion

In this tutorial, we explored and learned some interesting methods to remove duplicates from a list in Python.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263377.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论