Using Dictionaries in Sets - dict objects are unhashable?

  • Thread starter =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
  • Start date

?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

Hey guys,

I don't understand why this isn't working for me. I'd like to be able
to do this. Is there another short alternative to get this
intersection?

[Dbg]>>> set([{'a':1},{'b':2}]).intersection([{'a':1}])
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: dict objects are unhashable

(Bonus points: Why does anything have to be "hashed"? Can't it just
check the references?)

Thanks,
 
Ad

Advertisements

J

Jonathan Gardner

Welcome to Python, where all types are either static (and thus
hashable) or volatile (and thus not hashable.)

The way hash tables work (which is what powers the set and dict types),
you have to be able to get a number from an instance of the type, with
the following conditions true:

(1) Every equivalent instance (whatever that means to you) will give
you the same number for the type.

(2) You always get the same number for the instance of the type.
(Because things should be equal to themselves.)


If these two things don't hold true, then the hash tables won't work
like you would expect them to. (Imagine punching in the same dict that
has been modified slightly as the key. What do you expect to get back?)

You *could* create a dict that hashes off of its unique id. But the id
isn't really unique because old ids get recycled.

You *could* convert the dict to a tuple of key-value pairs, and then
hash that, but every value in that tuple would have to be hashable.

Instead of storing the dicts in the set, trying storing something else
that is hashable, and invent some way of "remembering" which thing
refers to which dict.
 
B

Ben Cartwright

Gregory said:
Hey guys,

I don't understand why this isn't working for me. I'd like to be able
to do this. Is there another short alternative to get this
intersection?

[Dbg]>>> set([{'a':1},{'b':2}]).intersection([{'a':1}])
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: dict objects are unhashable

Assuming you're using Python 2.4+:
{'a': 1, 'c': 3}

Or if you're comparing key/value pairs instead of just keys:
dict((k, v) for k, v in d1.iteritems() if k in d2 and d2[k]==v)
{'a': 1}

Finally, if you're on Python 2.3, use these versions (less efficient
but still functional):
dict([(k, v) for k, v in d1.iteritems() if k in d2]) {'a': 1, 'c': 3}
dict([(k, v) for k, v in d1.iteritems() if k in d2 and d2[k]==v])
{'a': 1}

--Ben
 
Ad

Advertisements

?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

Thanks guys. That was informative and helpful. I'm back on track now.

-Greg


Gregory said:
Hey guys,

I don't understand why this isn't working for me. I'd like to be able
to do this. Is there another short alternative to get this
intersection?

[Dbg]>>> set([{'a':1},{'b':2}]).intersection([{'a':1}])
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: dict objects are unhashable

Assuming you're using Python 2.4+:
{'a': 1, 'c': 3}

Or if you're comparing key/value pairs instead of just keys:
dict((k, v) for k, v in d1.iteritems() if k in d2 and d2[k]==v)
{'a': 1}

Finally, if you're on Python 2.3, use these versions (less efficient
but still functional):
dict([(k, v) for k, v in d1.iteritems() if k in d2]) {'a': 1, 'c': 3}
dict([(k, v) for k, v in d1.iteritems() if k in d2 and d2[k]==v])
{'a': 1}

--Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top