Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
confusion with hashable
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank You so much
Reply
#4
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?
Reply
#5
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).
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hashable confusion spalisetty06 3 1,948 Aug-16-2020, 03:54 PM
Last Post: spalisetty06

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020