Not sure why this one works when using set and zip:
>>> a = ([1])
>>> b = ([2])
>>> set(zip(a,b))
{(1, 2)}
but this one doesn’t ?.
>>> a = ([1],[2])
>>> b = ([3],[4])
>>> set(zip(a,b))
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
set(zip(a,b))
TypeError: unhashable type: 'list'
Desired result (1,3) (2,4)
What’s the right way to do this ?
Thanks!
John
It makes more sense if we look at the
zipoutput:In the first case, the list contains a tuple of ints; in the second case, it contains tuples of lists and lists are not hashable.
In the first case, if you wanted a singleton tuple containing a list, you should use
a = ([1],)andb = ([2],). If you defineaandbthat way, thenset(zip(a, b))will fail like it does in the second case.