set
can help you filter out the elements from the list that are duplicates. It will work well for str
, int
or tuple
elements, but if your list contains dict
or other list
elements, then you will end up with TypeError
exceptions.
Here is a general order-preserving solution to handle some (not all) non-hashable types:
def unique_elements(iterable):
seen = set()
result = []
for element in iterable:
hashed = element
if isinstance(element, dict):
hashed = tuple(sorted(element.iteritems()))
elif isinstance(element, list):
hashed = tuple(element)
if hashed not in seen:
result.append(element)
seen.add(hashed)
return result