PYTHON

Remove Duplicates from List While Preserving Order

Efficiently remove duplicate elements from a Python list without changing the original order of the remaining unique items, useful for maintaining sequence integrity in data processing.

def remove_duplicates_preserve_order(input_list):
    """Removes duplicate items from a list while preserving their original order."""
    # In Python 3.7+, dicts preserve insertion order, 
    # so dict.fromkeys creates keys in order of first appearance.
    return list(dict.fromkeys(input_list))

# Example Usage:
items = [1, 2, 2, 3, 1, 4, 5, 3, 6]
unique_items = remove_duplicates_preserve_order(items)

# print(unique_items)
# Expected output: [1, 2, 3, 4, 5, 6]

strings = ["apple", "banana", "orange", "apple", "grape", "banana"]
unique_strings = remove_duplicates_preserve_order(strings)

# print(unique_strings)
# Expected output: ['apple', 'banana', 'orange', 'grape']
How it works: This snippet provides a Pythonic and efficient way to remove duplicate elements from a list while maintaining the original order of the first occurrence of each element. It leverages the behavior of `dict.fromkeys()`, which creates a dictionary with elements from the iterable as keys. Since dictionary keys must be unique, duplicates are automatically removed. In Python 3.7 and later, dictionaries preserve insertion order, ensuring that `list(dict.fromkeys(input_list))` returns a list of unique elements in their original appearance order.

Need help integrating this into your project?

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

Hire DigitalCodeLabs