I was doing some coding exercises and I ended up using a set
of frozenset
s.
Here is the code:
cities = 4
roads = [[0, 1], [1, 2], [2, 0]]
roads = set([frozenset(road) for road in roads])
output = []
for i in range(cities-1):
for j in range(i+1, cities):
if set([i,j]) not in roads:
output.append([i,j])
As you can see, the if
in the nested for
tests for the presence of the set in the set of sets.
However, it was my understanding that in this case, hashables need to be used with the in
operator.
If I replace set([i,j])
with [i,j]
, I do get the following error:
TypeError: unhashable type: 'list'
So, here is my question: why does it work with the set, which is not (as far as I know) hashable and not with the list? Should it not also throw an error, what am I missing?