471,091 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,091 software developers and data experts.

Using Dictionaries in Sets - dict objects are unhashable?

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,

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
Mar 22 '06 #1
3 24170
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.

Mar 22 '06 #2
Gregory Piñero wrote:
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+:
d1 = {'a':1, 'b':2, 'c':3, 'd':5}
d2 = {'a':1, 'c':7, 'e':6}
dict((k, v) for k, v in d1.iteritems() if k in d2) {'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

Mar 22 '06 #3
Thanks guys. That was informative and helpful. I'm back on track now.

-Greg
On 21 Mar 2006 17:30:47 -0800, Ben Cartwright <be****@gmail.com> wrote:
Gregory Piñero wrote:
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+:
d1 = {'a':1, 'b':2, 'c':3, 'd':5}
d2 = {'a':1, 'c':7, 'e':6}
dict((k, v) for k, v in d1.iteritems() if k in d2) {'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

--
http://mail.python.org/mailman/listinfo/python-list

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
Mar 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Raymond Hettinger | last post: by
4 posts views Thread by Thomas Philips | last post: by
210 posts views Thread by Christoph Zwerschke | last post: by
9 posts views Thread by javuchi | last post: by
7 posts views Thread by ProvoWallis | last post: by
1 post views Thread by François Pinard | last post: by
15 posts views Thread by pretoriano_2001 | last post: by
13 posts views Thread by Martin Drautzburg | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.