Python is a versatile programming language that offers developers various functionalities. One common task that Python developers often encounter is removing duplicates from a list. This blog will explore 7 ways to remove duplicates from a list, along with code examples and explanations.
You can store multiple values in a single variable using lists, as they are ordered collections of items.
Key characteristics of a List:
Example 1:
# Creating a list
furnitures = ["chair", "table", "sofa"]
# Accessing elements by index (starts from 0)
first_furniture = furnitures[0]
print(first_furniture)
Output:
"chair"
Example 2:
last_furniture = furniture[-1] #(negative index starts from the end)
print(last_furniture)
Output:
"cherry"
# Modifying elements
furnitures[1] = "bed" # Replace "table" with "bed"
# Adding elements
furnitures.append("bed") # Add "bed" to the end
furnitures.insert(2, "table") # Insert "table" at index 2
# Removing elements
furnitures.remove("bed") # Remove the first occurrence of "cherry"
del furnitures[0] # Delete the first element
# Other common operations
furnitures.pop() # Remove and return the last element
furnitures.index("bed") # Find the index of "bed"
furnitures.count("table") # Count the occurrences of "table"
furnitures.sort() # Sort the list alphabetically
furnitures.reverse() # Reverse the order of the list
Also Read: Introduction to Python Programming (Beginner’s Guide)
The first method to remove duplicates from a list is using Python’s set() function. This method is simple and efficient. Assets do not allow duplicate elements.
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_set = set(my_list)
print(unique_set)
Output:
{1, 2, 3, 4, 5}
unique_list = list(unique_set)
print(unique_list)
Output:
[1, 2, 3, 4, 5]
Key points:
List comprehension is a powerful feature in Python that allows us to create new lists concisely and readably. We can leverage list comprehension to remove duplicates from a list.
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = [x for x in my_list if x not in unique_list]
print(unique_list)
Output:
[1, 2, 3, 4, 5]
Explanation:
Key points:
Python’s filter() function can be used to create a new list with elements that satisfy a certain condition. We can use the filter() function to remove duplicates from a list.
def is_unique(x, seen):
return x not in seen
2. Apply Filter() with a Set to Track Unique Elements
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(filter(lambda x: is_unique(x, set()), my_list))
print(unique_list)
Output:
[1, 2, 3, 4, 5]
Explanation:
Key points:
The OrderedDict class in Python is a dict subclass that remembers the order in which items are added. We can use the OrderedDict class to remove duplicates from a list while preserving the original order of elements.
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_dict = OrderedDict.fromkeys(my_list)
unique_list = list(unique_dict.keys())
print(unique_list)
Output:
[1, 2, 3, 4, 5]
Key points:
The Counter class in Python is a powerful tool for counting the occurrences of elements in a list. We can leverage the Counter class to remove duplicates from a list and obtain the count of each unique element.
my_list = [1, 2, 2, 3, 4, 4, 5]
counter = Counter(my_list)
unique_list = list(counter.keys())
print(unique_list)
Output:
[1, 2, 3, 4, 5]
Key points:
print(counter.most_common())
Output:
[(2, 2), (4, 2), (1, 1), (3, 1), (5, 1)]
The itertools module in Python provides a groupby() function that allows us to group elements based on a key function. We can use the groupby() function to remove duplicates from a list.
my_list = [1, 2, 2, 3, 4, 4, 5]
my_list.sort() # [1, 2, 2, 3, 4, 4, 5]
unique_elements = [key for key, _ in groupby(my_list)]
print(unique_elements)
Output:
[1, 2, 3, 4, 5]
Explanation:
Key points:
Use the pandas’ library in Python for data manipulation and analysis. We can use the dropduplicates() function in pandas to remove duplicates from a list and obtain the unique elements.
import pandas as pd
my_list = [1, 2, 2, 3, 4, 4, 5]
my_series = pd.Series(my_list)
unique_series = my_series.drop_duplicates()
unique_list = list(unique_series)
Output:
[1, 2, 3, 4, 5]
Key points:
Alternative using unique():
unique_list = my_series.unique() # Also returns a NumPy array
In this blog, we explored 7 ways to remove duplicates from a list in Python. Each method offers advantages and uses them based on the specific requirements of the task. By understanding these methods, Python developers can efficiently handle duplicate elements in lists and optimize their code for better performance. Whether it’s using set(), list comprehension, filter(), OrderedDict, Counter, itertools. group by (), or the panda’s library, Python provides a variety of tools to tackle the common problem of removing duplicates from a list.
If you want to learn Python from scratch. Then, enroll for free for this course.
A. Use the set()
constructor to eliminate duplicates in a list: unique_list = list(set(original_list))
.
Alternatively, employ a list comprehension: unique_list = [x for i, x in enumerate(original_list) if x not in original_list[:i]]
.
A. The fastest way to remove duplicates in Python is by using the set()
constructor: unique_list = list(set(original_list))
.
A. To efficiently remove duplicates from a list in Python while preserving the original order, you can use the collections.OrderedDict
approach:
from collections import OrderedDict
unique_list = list(OrderedDict.fromkeys(original_list))
A. To extract duplicates from a list in Python, you can use the following approach:
original_list = [1, 2, 2, 3, 4, 4, 5]
duplicates = [item for item in set(original_list) if original_list.count(item) > 1]
This list comprehension creates a new list (duplicates
) containing items that appear more than once in the original list while using a set to ensure unique values.