Python Program to Remove Duplicate Element From a List – 2023

Hey guys, in this blog we will see a Python Program to Remove Duplicate Element From a List.

Example 1: Using set()

list_1 = [2, 3, 2, 2, 4, 6]

print(list(set(list_1)))

Output

[2, 3, 4, 6]
  • Here we have first converted our list to a set.
  • We have done so because a set removes all the repetitions from a list.
  • Then we are again simply converting back the set to a list.

Example 2: Remove the items that are duplicated in two lists

list_1 = [2, 3, 4, 5]
list_2 = [4, 5, 6, 7]

print(list(set(list_1) ^ set(list_2)))

Output

[2, 3, 6, 7]
  • Here we have used the XOR operation between the two sets.
  • The XOR operation will make the duplicate items 0 as we know when the same things are XORed, they become 0.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 521

Leave a Reply

Your email address will not be published. Required fields are marked *