Interview Questions

How do you remove duplicates from a list?

Python Questions and Answers


(Continued from previous question...)

How do you remove duplicates from a list?

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 List:
List.sort()
last = List[-1]
for i in range(len(List)-2, -1, -1):
if last==List[i]: del List[i]
else: last=List[i]

If all elements of the list may be used as dictionary keys (i.e. they are all hashable) this is often faster

d = {}
for x in List: d[x]=x
List = d.values()

(Continued on next question...)

Other Interview Questions