PYTHON

Remove List Duplicates Preserving Order

Learn a concise and efficient Python method to remove duplicate elements from a list while maintaining their original order, ideal for data cleaning tasks.

# For Python 3.7+ (dict insertion order is guaranteed)
original_list = [1, 3, 2, 4, 3, 1, 5, 2, 6]
deduplicated_list_py37 = list(dict.fromkeys(original_list))
print(f"Python 3.7+ deduplicated list: {deduplicated_list_py37}")
# Expected: [1, 3, 2, 4, 5, 6]

# For older Python versions or more explicit control
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

deduplicated_list_generic = remove_duplicates_preserve_order(original_list)
print(f"Generic deduplicated list: {deduplicated_list_generic}")
# Expected: [1, 3, 2, 4, 5, 6]
How it works: This snippet offers two ways to remove duplicates while preserving the original order. For Python 3.7 and newer, `dict.fromkeys()` is a very concise and efficient method because dictionaries maintain insertion order. For older versions or when more explicit control is desired, iterating through the list and using a `set` to track seen items is a common and effective pattern.

Need help integrating this into your project?

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

Hire DigitalCodeLabs