PYTHON

Removing Duplicates from a List While Preserving Order

Learn to efficiently remove duplicate elements from a Python list while maintaining their original order, a common data cleaning task for web developers.

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:
my_list = [1, 2, 2, 3, 4, 2, 5, 1]
unique_list = remove_duplicates_preserve_order(my_list)
print(f"Original list: {my_list}")
print(f"List after removing duplicates: {unique_list}")
How it works: This snippet demonstrates how to remove duplicate items from a list while keeping the remaining elements in their original order. It achieves this by iterating through the input list and using a `set` named `seen` to efficiently track elements already encountered. If an item is not in `seen`, it's added to both `seen` and the `result` list. This approach leverages the O(1) average time complexity of set lookups for optimal performance.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs