PYTHON
Remove Duplicate Items from a List Preserving Order
Learn a Pythonic technique to eliminate duplicate elements from a list while maintaining the original order, useful for unique user selections or data clean-up in web dev.
original_list = [1, 2, 2, 3, 1, 4, 5, 3, 6, 7, 7]
original_strings = ['apple', 'banana', 'apple', 'orange', 'grape', 'banana']
# Using an ordered dictionary (Python 3.7+ guarantee order) or set for lookup
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
unique_numbers = remove_duplicates_preserve_order(original_list)
print(f"Original list: {original_list}")
print(f"Unique numbers (order preserved): {unique_numbers}")
unique_strings = remove_duplicates_preserve_order(original_strings)
print(f"Original strings: {original_strings}")
print(f"Unique strings (order preserved): {unique_strings}")
# For hashable items and Python 3.7+ (maintains insertion order for dict keys)
# Using dict.fromkeys is a more concise way for hashable items
unique_with_dict = list(dict.fromkeys(original_list))
print(f"Unique with dict.fromkeys: {unique_with_dict}")
How it works: This snippet provides methods to remove duplicate items from a list while ensuring their original order is preserved. The `remove_duplicates_preserve_order` function uses a `set` for efficient lookup of seen items. For lists containing only hashable items, the `list(dict.fromkeys(input_list))` approach offers a more concise solution (guaranteed order preservation from Python 3.7+ for dictionary keys). This is vital for maintaining data integrity and user experience when presenting lists of unique options or values in web applications.