Python Program to Remove Duplicates from a List

In this example, we will write a program to remove duplicate items in a list using Python programming. To better understand this example, make sure you have knowledge of the following tutorials:-

Python Program to Remove Duplicates from a List

a = [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]

uniq_items = []
for x in a:
    if x not in uniq_items:
        uniq_items.append(x)

print("Original List:", a)
print("Unique List:", uniq_items)

The output of the above program is:-

Original List: [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]
Unique List: [10, 20, 30, 50, 60, 40, 80]

Python Program to Remove Duplicates from a List using set() method

a = [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]

uniq_items = list(set(a))

print("Original List:", a)
print("Unique List:", uniq_items)

The output of the above program is:-

Original List: [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]
Unique List: [40, 10, 80, 50, 20, 60, 30]

Program Explanation:
Here, we define a list with several items that contain duplicate entries. To create a unique list, a new list is initialized with an empty value. If each of the element is not in the new list, then it is inserted else the loop is skipped and moved to next item in the list. In this way, we get only unique values in our new list.
SHARE Python Program to Remove Duplicates from a List

You may also like...

Leave a Reply

Your email address will not be published.

Share