How to get unique of unhashable list in python

Question:

I have a list of items below.

a=[['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR08-C2-p20'], ['JR08-C2-p20'], ['JR08-C2-p20'], ['JR08-C2-p20'], ['JR08-C2-p20']]

When I tried to get unique of a as list(set(a)), I get TypeError: unhashable type: 'list'. How do I get the solution for this?

Asked By: MAPK

||

Answers:

If you don’t care about the order, you could convert the data to a hashable type, create a set, and then convert back to lists.

a = [
    ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'],
    ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'],
    ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'],
    ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'], ['JR06-C5-p21'],
    ['JR08-C2-p20'], ['JR08-C2-p20'], ['JR08-C2-p20'], ['JR08-C2-p20'],
    ['JR08-C2-p20']
]

a = list(map(list, set(map(tuple, a))))
print(a)
[['JR08-C2-p20'], ['JR06-C5-p21']]

If insertion order matters, you can do a similar trick as here, with the adaptation that you still transform the inner data to a hashable type:

def filter_unique(seq):
    seen = set()
    return [
        x for x in seq
        if not ((hashable := tuple(x)) in seen or seen.add(hashable))
    ]


print(filter_unique(a))
[['JR06-C5-p21'], ['JR08-C2-p20']]
Answered By: flakes
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.