← Back to all snippets
PYTHON

Remove Duplicates from a List Preserving Order

Learn how to efficiently remove duplicate elements from a Python list while maintaining their original order, a crucial task for data cleaning in web development.

def remove_duplicates_preserve_order(input_list):
    return list(dict.fromkeys(input_list))

my_list = [1, 2, 2, 3, 1, 4, 5, 3]
unique_ordered_list = remove_duplicates_preserve_order(my_list)
# Result: [1, 2, 3, 4, 5]

another_list = ["apple", "banana", "apple", "cherry", "banana"]
unique_fruits = remove_duplicates_preserve_order(another_list)
# Result: ['apple', 'banana', 'cherry']
How it works: This snippet demonstrates an elegant and efficient method to remove duplicate items from a list while ensuring their original insertion order is preserved. It leverages the property of Python dictionaries (from Python 3.7+ onwards, officially ordered) that when `dict.fromkeys()` is called with an iterable, it creates a dictionary where each unique item from the iterable becomes a key. Because dictionary keys must be unique and preserve insertion order, converting this dictionary back to a list yields the desired result, making it perfect for cleaning up retrieved data.

Need help integrating this into your project?

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

Hire DigitalCodeLabs