Remove Duplicates from a List in Python



To remove duplicates from a List in Python, we can use various ways as discussed in this article.

Remove duplicates from a list using Dictionary

Example

In this example, we will remove duplicates from a list using OrderedDict ?

from collections import OrderedDict # Creating a List with duplicate items mylist = ["Jacob", "Harry", "Mark", "Anthony", "Harry", "Anthony"] # Displaying the List print("List = ",mylist) # Remove duplicates from a list using dictionary resList = OrderedDict.fromkeys(mylist) # Display the List after removing duplicates print("Updated List = ",list(resList))

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony', 'Harry', 'Anthony']
Updated List =  ['Jacob', 'Harry', 'Mark', 'Anthony']

Remove duplicates from a list using the List Comprehension

Example

In this example, we will remove duplicates from a list using the List Comprehension ?

# Creating a List with duplicate items mylist = ["Jacob", "Harry", "Mark", "Anthony", "Harry", "Anthony"] # Displaying the List print("List = ",mylist) # Remove duplicates from a list using List Comprehension resList = [] [resList.append(n) for n in mylist if n not in resList] print("Updated List = ",resList)

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony', 'Harry', 'Anthony']
Updated List =  ['Jacob', 'Harry', 'Mark', 'Anthony']

Remove duplicates from a list using Set

Example

In this example, we will remove duplicates from a list using the set() method ?

# Creating a List with duplicate items mylist = ["Jacob", "Harry", "Mark", "Anthony", "Harry", "Anthony"] # Displaying the List print("List = ",mylist) # Remove duplicates from a list using Set resList = set(mylist) print("Updated List = ",list(resList))

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony', 'Harry', 'Anthony']
Updated List =  ['Anthony', 'Mark', 'Jacob', 'Harry']
Updated on: 2022-09-16T12:21:39+05:30

492 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements