When working with lists in
Python, you may encounter duplicate elements. Removing these duplicates ensures each item appears only once. A simple and efficient way to remove duplicates is by converting the list into a
set, as sets in Python automatically store only unique elements. However, using a set does not preserve the original order of the list.
If maintaining the original order is important, you can use a different approach by iterating through the list, checking if an element has already been seen, and adding it to a new list if it has not. This way, the first occurrence of each element is retained, and duplicates are ignored.
Here’s how both concepts work:
Set approach: Quick, but does not guarantee order.
Manual checking with a loop: Slightly slower, but maintains the input order.
Depending on your requirements (whether you need to maintain the original order or not), you can pick either method.
Below is a
Python code example that removes duplicates from a list while maintaining the order. It uses a
set to track seen elements and a new_list to store results.
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
# Example usage
original_list = [1, 2, 2, 3, 4, 4, 5, 1, 6]
result = remove_duplicates(original_list)
print("Original List:", original_list)
print("List after removing duplicates:", result)
Explanation of Code:
seen keeps track of elements that have already appeared.
Only unseen elements are added to unique_list.
This ensures the first occurrence is kept and duplicates are ignored.
The output for the above example will be:
Original List: [1, 2, 2, 3, 4, 4, 5, 1, 6]
List after removing duplicates: [1, 2, 3, 4, 5, 6]
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
When working with lists in Python, you may encounter duplicate elements. Removing these duplicates ensures each item appears only once. A simple and efficient way to remove duplicates is by converting the list into a set, as sets in Python automatically store only unique elements. However, using a set does not preserve the original order of the list.
If maintaining the original order is important, you can use a different approach by iterating through the list, checking if an element has already been seen, and adding it to a new list if it has not. This way, the first occurrence of each element is retained, and duplicates are ignored.
Here’s how both concepts work:
Depending on your requirements (whether you need to maintain the original order or not), you can pick either method.
Below is a Python code example that removes duplicates from a list while maintaining the order. It uses a
setto track seen elements and anew_listto store results.Explanation of Code:
seenkeeps track of elements that have already appeared.unique_list.The output for the above example will be: