How to make a set of lists

You can convert each element to a tuple and then insert it in a set.

Here's some code with your example:

tmp = set()
a = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
for i in a:
    tmp.add(tuple(i))

tmp will be like this:

{(1, 2, 3), (2, 4, 5)}

i = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]

print([ele for ind, ele in enumerate(i) if ele not in i[:ind]])
[[1, 2, 3], [2, 4, 5]]

If you consider [2, 4, 5] to be equal to [2, 5, 4] then you will need to do further checks


You can create a set of tuples, a set of lists will not be possible because of non hashable elements as you mentioned.

>>> l = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
>>> set(tuple(i) for i in l)
{(1, 2, 3), (2, 4, 5)}

Tags:

Python

List

Set