Show three ways to remove duplicates from a Python list, with their trade-offs.
There are three common approaches to removing duplicates from a list, and they differ mainly in whether they keep the original order. The question before this one covered how lists work, so this answer builds on that and focuses on the choice between the three methods.
items = [3, 1, 3, 2, 1, 4]
# 1. With a set. This is the fastest, but the order is lost.
unique_set = list(set(items))
print(len(unique_set)) # 4
# 2. With dict.fromkeys. This keeps the order and it is still fast.
unique_dict = list(dict.fromkeys(items))
print(unique_dict) # [3, 1, 2, 4]
# 3. With a plain loop. This keeps the order and it is the clearest.
seen = set()
unique_loop = []
for item in items:
if item not in seen:
seen.add(item)
unique_loop.append(item)
print(unique_loop) # [3, 1, 2, 4]
The set version is the shortest and the fastest of the three, but the order of the result is whatever the hash table happens to produce. You cannot rely on that order, and it looks random to anyone who reads the code later.
The dict.fromkeys version is the one to reach for in most situations. Since Python 3.7, a dictionary keeps its keys in the order they were added. So dict.fromkeys(items) builds a dictionary whose keys are the first appearance of each item, in the order the items first appeared, and whose values are all None. Turning that back into a list with list() gives you the list without duplicates and with the original order intact. It runs at the same speed as the set version, and it is only one line.
The plain loop is the most readable of the three, and it makes the intent obvious to anybody who reads it. This is also the version to write when you also need to count how many times each item appears, because you can swap the set of seen items for a counter and get a frequency table instead.
There are two limits to keep in mind. All three approaches need the items to be hashable, so a list that contains another list cannot be handled this way at all. And if you need the order of the last appearance rather than the first, you can reverse the list, remove the duplicates, and reverse the result again. That last request shows up often in interview questions.