How to Remove Duplicate Values from a List in Python
How to Remove Duplicate Values from a List in Python
How to Remove Duplicate Values from a List in Python
I have a list with duplicate values, and I need to remove the duplicates while maintaining the order of elements. How can I achieve this?
solveurit24@gmail.com Changed status to publish February 16, 2025
Removing duplicates from a list in Python while preserving the order of elements can be done using a combination of a loop and a set. Here’s how you can do it:
- Using a Set to Track Duplicates:
- Create an empty set to keep track of elements that have already been seen.
- Create a new list to store the elements without duplicates.
- Iterate through the original list, and for each element, check if it’s in the set.
- If it’s not in the set, add it to the set and the new list.
- Code Example:
my_list = [1, 2, 2, 3, 4, 4, 4, 5] seen = set() unique_list = [] for item in my_list: if item not in seen: seen.add(item) unique_list.append(item) print("Original list:", my_list) print("List without duplicates:", unique_list) - Alternative Method for Python 3.7+:
- If you’re using Python 3.7 or newer, you can leverage the insertion-order preservation feature of dictionaries. Use the
dict.fromkeys()method.
my_list = [1, 2, 2, 3, 4, 4, 4, 5] unique_list = list(dict.fromkeys(my_list)) print("List without duplicates:", unique_list) - If you’re using Python 3.7 or newer, you can leverage the insertion-order preservation feature of dictionaries. Use the
Explanation:
- Using a Set: This method ensures that you track elements efficiently, as sets have average O(1) time complexity for membership checks.
- Preserving Order: Both methods maintain the order of elements as they appeared in the original list.
These techniques allow you to remove duplicates from a list while preserving the order, depending on your Python version and use case.
solveurit24@gmail.com Changed status to publish February 16, 2025