How do you remove duplicates from a list?
PythonLevel: Intermediate
How do you remove duplicates from a list?
Hints
- Identify object types, mutability and execution order.
- Trace a minimal example: which values and references change?
Answer
See the Python Cookbook for a long discussion of many ways to do this:
https://code.activestate.com/recipes/52560/
If you don’t mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go:
if mylist:
mylist.sort()
last = mylist[-1]
for i in range(len(mylist)-2, -1, -1):
if last == mylist[i]:
del mylist[i]
else:
last = mylist[i]
If all elements of the list may be used as set keys (i.e. they are all hashable) this is often faster
mylist = list(set(mylist))
This converts the list into a set, thereby removing duplicates, and then back into a list.
Source and licence: Python 3.14.0 Programming FAQ — Python Software Foundation · PSF-2.0