PYTHON

Remove List Duplicates While Preserving Order

Learn a Pythonic way to remove duplicate elements from a list without changing the original order of the remaining unique items, using sets and dictionary features.

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

# Method 1: Using dict.fromkeys (most Pythonic and efficient for hashable items)
unique_list_ordered_dict = list(dict.fromkeys(my_list))
print(f"Original list: {my_list}")
print(f"Unique (dict.fromkeys, preserving order): {unique_list_ordered_dict}")

# Method 2: Using a set and list comprehension (good if order is less critical or for non-hashable, though dict.fromkeys is superior here)
# This requires an intermediate step or careful tracking
seen = set()
unique_list_ordered_comprehension = [x for x in my_list if x not in seen and not seen.add(x)]
print(f"Unique (set + comprehension, preserving order): {unique_list_ordered_comprehension}")
How it works: A common task is to remove duplicates from a list while maintaining the original order of the unique elements. The most Pythonic and efficient method for hashable items is to use `dict.fromkeys(iterable)`. This creates a dictionary where each unique element from the iterable becomes a key, preserving insertion order from Python 3.7+, and then converting the dictionary keys back to a list. An alternative using a `set` to track seen items with a list comprehension is also shown.

Need help integrating this into your project?

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

Hire DigitalCodeLabs