PYTHON
Efficiently Remove Duplicates from a List While Preserving Order
Learn to remove duplicate elements from a Python list while maintaining their original order using a combination of sets and lists for efficient processing.
def remove_duplicates_preserve_order(input_list):
seen = set()
result = []
for item in input_list:
if item not in seen:
seen.add(item)
result.append(item)
return result
# Example usage:
data = [1, 3, 2, 4, 3, 1, 5, 2]
unique_ordered_data = remove_duplicates_preserve_order(data)
# unique_ordered_data will be [1, 3, 2, 4, 5]
print(f"Original list: {data}")
print(f"Unique and ordered list: {unique_ordered_data}")
data_strings = ["apple", "banana", "apple", "orange", "banana"]
unique_ordered_strings = remove_duplicates_preserve_order(data_strings)
# unique_ordered_strings will be ["apple", "banana", "orange"]
print(f"Original strings: {data_strings}")
print(f"Unique and ordered strings: {unique_ordered_strings}")
How it works: This snippet defines a function `remove_duplicates_preserve_order` that takes a list and returns a new list with duplicates removed, preserving the order of the first occurrence. It uses a `set` named `seen` for efficient O(1) average-case lookup to track elements already encountered, and a `list` named `result` to store the unique elements in their original order.