Python How To Remove Duplicates 🗑️
Remove Duplicates From a List
Learn how to remove duplicates from a list in Python.
✨ Using a Dictionary
You can remove duplicates by converting the list into a dictionary, using the list items as keys. This will automatically remove any duplicates because dictionaries cannot have duplicate keys.
python
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)✨ Using a Set
You can also remove duplicates by converting the list into a set. Sets cannot have two items with the same value.
python
mylist = ["a", "b", "a", "c", "c"]
mylist = list(set(mylist))
print(mylist)💡 Did You Know?
Converting to a set is faster, but it does not keep the original order of the items.