Reviewed by Aditya Kumar · Last reviewed 2026-03-24
To remove duplicates from a list while maintaining order in Python, the most efficient and Pythonic approaches leverage either the dict.fromkeys() method (Python 3.7+) or a combination of a set for…
This easy-level Python/Coding question appears frequently in data engineering interviews at companies like Delivery Hero. While less common, it tests deeper understanding that distinguishes strong candidates.
Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.
To remove duplicates from a list while maintaining order in Python, the most efficient and Pythonic approaches leverage either the dict.fromkeys() method (Python 3.7+) or a combination of a set for tracking seen elements and a new list for results. Both methods achieve an average time complexity of O(n).
list(dict.fromkeys(lst)) method is concise and often the fastest for Python 3.7 and later. Python dictionaries guarantee insertion order preservation since version 3.7. When dict.fromkeys(lst) is called, it iterates through lst, adding each unique element as a key to a new dictionary. Since duplicate keys are ignored (the first occurrence is kept), the resulting dictionary's keys represent the unique elements in their original order. Converting this dictionary back to a list yields the desired result.
Alternatively, a set-based approach involves iterating through the original list. For each element, we check if it's already present in a seen set. If not, we add it to our result list and to the seen set. Set lookups and insertions are, on average, O(1) operations, leading to an overall O(n) time complexity for processing the list once. Both methods incur O(n) space complexity in the worst case (all elements are unique).
dict.fromkeys() method is generally preferred for its readability and performance.
my_list = [1, 2, 2, 3, 1, 4, 5, 4]
unique_ordered_list = list(dict.fromkeys(my_list))
# Result: [1, 2, 3, 4, 5]
A critical trade-off for both set and dict based solutions is that the elements in the list must be hashable. Hashable types include numbers, strings, and tuples. Unhashable types like lists, dictionaries, or custom objects without a defined __hash__ method will raise a TypeError. For unhashable elements, a nested loop comparison (O(n^2)) or converting elements to a hashable representation (if possible) would be necessary, but these are less efficient.
df.distinct() are used, though explicit order preservation often requires a subsequent sort.Pro-Move: dict.fromkeys. Red Flag: Converting to set (loses order).
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.