Python Forum
confusion with hashable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: confusion with hashable (/thread-29154.html)



confusion with hashable - spalisetty06 - Aug-20-2020

Hello, I have the following code, kindly help what is going on in a simple 1 or 2 lines. set is not hashable, I accept it. dict is not hashable, list is also not hashable, but, why is that we don't get error for list?
Also, why is that I am getting 'set' object is not subscriptable. Please don't mind, it might be stupid questions for you.

s = {{1, 2},{3, 4} }
print(s[0][1])
Output:
s = {{1, 2},{3, 4} } TypeError: unhashable type: 'set'
l = [[1, 2], [1, 2]]
print(l[0][1])
Output:
2
d = {
        'cars': {'Innova', 'Honda City', 'BMW'},
        'phones': {'Iphone', 'Samsung', 'Nokia'}
     }
print(d['cars'][0])
Output:
print(d['cars'][0]) TypeError: 'set' object is not subscriptable



RE: confusion with hashable - buran - Aug-20-2020

these are 2 different errors
the first one - TypeError: unhashable type: 'set' - you get because elements of a set MUST be hashable.
the second error - TypeError: 'set' object is not subscriptable - you have a dict where values are set. You try to access element of such set (the value for key 'cars') by index - that is not possible. sets are not subscriptable (i.e. you cannot use [] to access element), they are unordered collection

the list of list example does not raise any error - it's perfectly fine to have unhashable element in a list


RE: confusion with hashable - spalisetty06 - Aug-20-2020

Thank You so much


RE: confusion with hashable - spalisetty06 - Aug-21-2020

Hi Sir, thank you for helping me out, am I very happy now, but for this.

'''tuple inside set'''
s = {(1, 2), (1, 2)}
print(s[0][1])
Output:
TypeError 'set' object is not subscriptable
I am good here, because with set, we can't predict the order, we may not use it

here is problem

''list inside set'''
s = {[1, 2, 3], [3, 4, 5]}
print(s[0])
Output:
TypeError: unhashable type: 'list'
here I am using set only, but I think, I should get the same output as ago, why is it complaining about list?


RE: confusion with hashable - ndc85430 - Aug-21-2020

Because lists are mutable. As you've been told above, the elements of a set must be hashable. Mutable things are not hashable, because if you were to mutate them, their hash value would change meaning they'd end up being in the wrong bucket in the table (of course, you could move them, but it would likely affect performance).


RE: confusion with hashable - deanhystad - Aug-21-2020

If you have a set s = {1, 2, 3} and ask for s[0] you will get an error. Sets are unordered and indexing implies that the thing being indexed has an order. You can iterate over the set, but don't expect the order of items to be the same as the order the items were added to the set. The items will be ordered by hash value.

When trying to create a set of lists, the problem is, as mentioned previously, that lists are not hashable. You cannot create a set of lists. Now your program has two errors. You are trying to make a set that contains items that are not hashable, and you are trying to index a set which is not a valid operation. The unhashable type error occurs first, so that is the one that gets reported.

I try to only use sets when I want to do set operations. Otherwise there are too many limitations with sets.