Often we would like to remove duplicate items from a list in Python. We can achieve this using one of two approaches.
In the first approach, we simply convert our
list to a
set, and then back to a
list again (line #16). Because sets only contain distinct elements, all duplicates are removed. The downside to this approach is that it does not necessarily maintain the order of the original list.
If order is important, you can instead convert the list to a
dict using the
fromkeys method, and convert this
dict back to a
list (line #26). This approach is guaranteed to maintain order in Python 3.6+, as dictionaries are insertion ordered. For compatibility with older versions of Python, we can use
OrderedDict instead (line #21).
See the official Python documentation for details on