Python | Program to remove duplicate elements from the list

Python | Remove duplicates from a list: In this tutorial, we will learn how to remove duplicate elements from the list in Python using multiple methods. By IncludeHelp Last updated : June 22, 2023

Given a Python list, we have to remove duplicate elements from it.

Let's consider the below example with sample input and output:

Input:
list1:  [10, 20, 10, 20, 30, 40, 30, 50]

Output:
List after removing duplicate elements
list2:  [10, 20, 30, 40, 50]

Remove duplicate elements from the Python list

There can be multiple approaches to remove duplicate elements from a given Python list. Here, we are discussing the following approaches:

  1. Approach #1: Using not in on list
  2. Approach #2: Using set data structure
  3. Approach #3: Using list() and dict.fromkeys() methods

Approach #1: Using not in on list

To implement the program is too easy, we have to append elements one by one to another list by checking whether element is available in the new list or not.

Let suppose, 20 is available three times in the list list1 and when we append 20 (first occurrence) to the list list2, it will be appended, but when we append 20 (second occurrence) to the list list2, condition will be false and item will not be appended. And finally, we will get list without duplicate elements.

Example

# declare list
list1 = [10, 20, 10, 20, 30, 40, 30, 50]

# creating another list with unique elements
# declare another list
list2 = []

# appending elements
for n in list1:
    if n not in list2:
        list2.append(n)

# printing the lists
print("Original list")
print("list1: ", list1)

print("List after removing duplicate elements")
print("list2: ", list2)

Output

Original list
list1:  [10, 20, 10, 20, 30, 40, 30, 50]
List after removing duplicate elements
list2:  [10, 20, 30, 40, 50]

Approach #2: Using set data structure

To remove duplicates from a list, you can use a set data structure. In which convert the list to the set and again convert it into a list using the set() and list() methods receptively. This will return a list after removing the duplicate elements.

Example

# declare list
list1 = [10, 20, 10, 20, 30, 40, 30, 50]

# removing duplicates
newlist = list(set(list1))

# printing the original and new list
print("Original list:")
print(list1)

print("New list after removing duplicates:")
print(newlist)

Output

Original list:
[10, 20, 10, 20, 30, 40, 30, 50]
New list after removing duplicates:
[40, 10, 50, 20, 30]

Approach #3: Using list() and dict.fromkeys() methods

To remove duplicates from a list, you can also use the dict.fromkeys() method by passing the list and then convert the result to the list again using the list() method. Let's consider the below example.

Example

# declare list
list1 = [10, 20, 10, 20, 30, 40, 30, 50]

# removing duplicates using list() 
# and dict.fromkeys() methods
newlist = list(dict.fromkeys(list1))

# printing the original and new list
print("Original list:")
print(list1)

print("New list after removing duplicates:")
print(newlist)

Output

Original list:
[10, 20, 10, 20, 30, 40, 30, 50]
New list after removing duplicates:
[10, 20, 30, 40, 50]

Python List Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.