473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 24336
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
4
by: Thomas Philips | last post by:
I'm teaching myself python and in the course of playing around with dictionaries, I tried to create the following trivial dictionary {1:'one', 2:'two'} So I entered >>> dict(1='one',2='two')...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
9
by: javuchi | last post by:
I've been searching thru the library documentation, and this is the best code I can produce for this alogorithm: I'd like to return a dictionary which is a copy of 'another' dictionary whoes...
7
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using...
1
by: François Pinard | last post by:
Hi, people. I noticed today that dictionaries seem to support `==' comparison. (Retrospectively, it is strange that I never needed it before! :-) Yet, before relying on this, I seeked for...
15
by: pretoriano_2001 | last post by:
Hello: I have next dictionaries: a={'a':0, 'b':1, 'c':2, 'd':3} b={'a':0, 'c':1, 'd':2, 'e':3} I want to put in a new dictionary named c all the keys that are in b and re-sequence the values....
3
by: ZMY | last post by:
I am new to Numpy/Pylab, and I am trying to construct a list of dictionaries with arrays as the items, for example: .... ), 2: ''}, {1: array(), 2: ''}, {1: array(), 2: ''}] ), 2: ''}, {1:...
13
by: Martin Drautzburg | last post by:
This may be pretty obvious for most of you: When I have an object (an instance of a class "Foo") I can access attributes via dot notation: aFoo.bar however when I have a dictionary aDict...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.