PYTHON

Remove Duplicates from List (Order Preserved)

Learn to efficiently remove duplicate elements from a Python list while strictly preserving the original order of the remaining unique items, useful for maintaining data sequence.

my_list = [1, 5, 2, 6, 1, 3, 5, 4, 2, 7]

# Method 1: Using dict.fromkeys (Python 3.7+ guarantees order)
def remove_duplicates_ordered_dict(input_list):
    return list(dict.fromkeys(input_list))

unique_list_1 = remove_duplicates_ordered_dict(my_list)
print(f"Method 1 (dict.fromkeys): {unique_list_1}")

# Method 2: Manual approach using a set for tracking and a list for output
def remove_duplicates_manual(input_list):
    seen = set()
    result = []
    for item in input_list:
        if item not in seen:
            seen.add(item)
            result.append(item)
    return result

unique_list_2 = remove_duplicates_manual(my_list)
print(f"Method 2 (Manual): {unique_list_2}")

# Expected Output:
# Method 1 (dict.fromkeys): [1, 5, 2, 6, 3, 4, 7]
# Method 2 (Manual): [1, 5, 2, 6, 3, 4, 7]
How it works: Removing duplicates from a list while preserving the original order is a common requirement. The first method, `list(dict.fromkeys(input_list))`, is concise and leverages the fact that dictionary keys must be unique and, from Python 3.7 onwards, dictionaries preserve insertion order. The second method provides a manual, explicit approach using a `set` to track seen elements for O(1) average time complexity lookups, and a `list` to build the result, ensuring order is maintained.

Need help integrating this into your project?

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

Hire DigitalCodeLabs