Remove Duplicates in two-dimensional array while maintaining sequence

arrays, python

Solution

Using `set` to keep track of seen items:

>>> mylist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
>>> seen = set()
>>> newlist = []
>>> for item in mylist:
...     t = tuple(item)
...     if t not in seen:
...         newlist.append(item)
...         seen.add(t)
...
>>> newlist
[['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]

NOTE

You need to convert a list to tuple (list is not hashable); can't add a list to set.

>>> seen = set()
>>> seen.add([1,2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> seen.add(tuple([1,2]))
>>>

Problem

I have found a lot of threads on removing duplicates in arrays but none for my specific use-case. I have a two-dimensional list that I need to remove duplicates from however I must maintain the original sequence ``` mylist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']] ``` I need to simply drop the duplicates without re-arranging, so.. ``` newlist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']] ``` appreciate any help

Original source