
👉How to Remove Duplicates from a List in Python: Complete Guide with Examples
How to Remove Duplicates from a List in Python: In Python, lists often contain duplicate values. Removing these duplicates efficiently is crucial for cleaner data and improved performance. This guide covers 8 powerful methods to remove duplicates from a list with detailed examples.
👉How to Remove Duplicates from a List in Python Table of Contents
- Introduction to Removing Duplicates in Python
- Method 1: Using set()
- Method 2: Using a Temporary List
- Method 3: Using OrderedDict
- Method 4: Using a For-loop
- Method 5: Using List Comprehension
- Method 6: Using NumPy’s unique() Method
- Method 7: Using Pandas’ unique() Method
- Method 8: Using enumerate() and List Comprehension
- Summary
👉Introduction to Removing Duplicates in Python
How to Remove Duplicates from a List in Python: Python lists can store multiple elements, including integers, strings, and objects. Duplicates may arise when processing data, making it essential to know effective ways to remove them.
How to Remove Duplicates from a List in Python Methods
👉Method 1: Using set()
How to Remove Duplicates from a List in Python: The set() method automatically removes duplicate values since sets only store unique elements.
Example Code:
python
my_list = [1, 1, 2, 3, 2, 2, 4, 5, 6, 2, 1]
my_final_list = list(set(my_list))
print(my_final_list)
Output:
csharp
[1, 2, 3, 4, 5, 6]
👉Method 2: Using a Temporary List
This method involves iterating through the list and adding unique items to a temporary list.
Example Code:
python
my_list = [1, 2, 3, 1, 2, 4, 5, 4, 6, 2]
temp_list = []
for i in my_list:
if i not in temp_list:
temp_list.append(i)
print(“List After Removing Duplicates:”, temp_list)
Output:
csharp
[1, 2, 3, 4, 5, 6]
👉Method 3: Using OrderedDict
OrderedDict maintains the original order of elements while removing duplicates.
Example Code:
python
from collections import OrderedDict
my_list = [‘a’, ‘x’, ‘a’, ‘y’, ‘a’, ‘b’, ‘b’, ‘c’]
my_final_list = list(OrderedDict.fromkeys(my_list))
print(my_final_list)
Output:
css
[‘a’, ‘x’, ‘y’, ‘b’, ‘c’]
👉Method 4: Using a For-loop
This method iterates through the list, checking each item’s presence in the new list.
Example Code:
python
my_list = [1, 2, 2, 3, 1, 4, 5, 1, 2, 6]
myFinallist = []
for i in my_list:
if i not in myFinallist:
myFinallist.append(i)
print(myFinallist)
Output:
csharp
[1, 2, 3, 4, 5, 6]
👉Method 5: Using List Comprehension
List comprehension offers a concise way to filter duplicates.
Example Code:
python
my_list = [1, 2, 2, 3, 1, 4, 5, 1, 2, 6]
my_finallist = []
[my_finallist.append(n) for n in my_list if n not in my_finallist]
print(my_finallist)
Output:
csharp
[1, 2, 3, 4, 5, 6]
👉Method 6: Using NumPy’s unique() Method
NumPy’s unique() method efficiently handles numerical data for duplicate removal.
Example Code:
python
import numpy as np
my_list = [1, 2, 2, 3, 1, 4, 5, 1, 2, 6]
myFinalList = np.unique(my_list).tolist()
print(myFinalList)
Output:
csharp
[1, 2, 3, 4, 5, 6]
👉Method 7: Using Pandas’ unique() Method
For data analysis tasks, Pandas’ unique() method is highly effective.
Example Code:
python
import pandas as pd
my_list = [1, 2, 2, 3, 1, 4, 5, 1, 2, 6]
myFinalList = pd.unique(my_list).tolist()
print(myFinalList)
Output:
csharp
[1, 2, 3, 4, 5, 6]
👉Method 8: Using enumerate() and List Comprehension
This method combines enumerate() with list comprehension for an efficient solution.
Example Code:
python
my_list = [1, 2, 2, 3, 1, 4, 5, 1, 2, 6]
my_finallist = [i for j, i in enumerate(my_list) if i not in my_list[:j]]
print(my_finallist)
Output:
csharp
[1, 2, 3, 4, 5, 6]
👉How to Remove Duplicates from a List in Python Summary
✅ Using set() offers a quick and efficient solution for removing duplicates.
✅ The OrderedDict method is ideal when maintaining order is crucial.
✅ NumPy and Pandas methods are best suited for large datasets.
✅ For-loop and list comprehension provide greater flexibility for customized logic.