Removing duplicates from a list is a common task in data processing and analysis. In Python, there are several ways to achieve this, such as using sets, list comprehensions, or libraries like collections
. Using a set is one of the simplest and most efficient methods, as sets inherently do not allow duplicate values. Here, we’ll demonstrate how to remove duplicates from a list using a set and also maintain the original order of elements.
# Define a list with duplicate elements
numbers = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10]
# Use a set to remove duplicates while maintaining order
def remove_duplicates(lst):
seen = set()
unique_list = []
for item in lst:
if item not in seen:
unique_list.append(item)
seen.add(item)
return unique_list
# Remove duplicates from the list
unique_numbers = remove_duplicates(numbers)
# Print the resulting list of unique numbers
print(f"Unique numbers: {unique_numbers}")
A list numbers
is defined, containing several duplicate elements.
numbers = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10]
A function remove_duplicates
is defined that takes a list as its argument and returns a new list with duplicates removed while maintaining the original order.
def remove_duplicates(lst):
seen = set()
unique_list = []
for item in lst:
if item not in seen:
unique_list.append(item)
seen.add(item)
return unique_list
seen
is used to keep track of elements that have already been encountered.unique_list
is used to store the unique elements in their original order.lst
.seen
set, it is appended to unique_list
, and the item is added to the seen
set.The remove_duplicates
function is called with the numbers
list to remove duplicates.
unique_numbers = remove_duplicates(numbers)
The resulting list of unique numbers is printed.
print(f"Unique numbers: {unique_numbers}")