Phillip Phillip - 5 months ago 8
Python Question

Distinct List of Lists With Set

I created a list of lists and then tried to get a distinct list of the lists using set(), but it appears as though i cant use list on a set.

Is there another way to accomplish this with a concise statement that performs well?

CODE

x = [1,2]
y = [1,2]
z = [2,3]

xyz = []

xyz.append(x)
xyz.append(y)
xyz.append(z)

set(xyz)


Error

TypeError: unhashable type: 'list'


Goal

xyz = [[1,2],[2,3]]

Answer

If the list members are hashable, it will work

x = [1,2]
y = [1,2]
z = [2,3]
xyz = []

xyz.append(tuple(x))
xyz.append(tuple(y))
xyz.append(tuple(z))

print xyz
xyz_set = set(xyz)
print xyz_set