In Python list, remove duplicates by:
Output:
['b', 'a', 'c']
Or if you care about the ordering, do it like this:
Output:
['a', 'b', 'c']
This approach works by converting a list into a dictionary and then right back into a list.
It works because a dictionary (or an ordered dictionary) cannot have duplicate keys. Thus, the dict.fromkeys()
method has to remove duplicates before converting the list into a dictionary.
After the conversion, the dictionary has all the list elements as keys without duplicates. Converting this dictionary back to a list gives you back the original list with duplicates removed.
In case you didn’t like these approaches or want to explore more ways to remove duplicates, here are some for you:
#data-science #programming #python #money #finance