
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How do you 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']
- Related Articles
- How do you remove multiple items from a list in Python?
- Python - Ways to remove duplicates from list
- Python program to remove Duplicates elements from a List?
- Remove duplicates from a List in C#
- Remove Duplicates from Sorted List in C++
- How to remove duplicates from a sorted linked list in android?
- Remove Duplicates from Sorted List II in C++
- Java program to remove duplicates elements from a List
- Remove Duplicates from a Sorted Linked List Using Recursion
- Remove Duplicates from Sorted Array in Python
- Remove all duplicates from a given string in Python
- How to Automatically Remove Duplicates form a List in Excel?
- Java Program to Remove Duplicates from an Array List
- Remove Consecutive Duplicates in Python
- How do I remove multiple elements from a list in Java?

Advertisements