I just ran across something interesting that I thought I'd ask about.
Adding a dictionary to a set
, I had assumed that the dictionary would be added as a full dictionary, it isn't. Only the keys are added:
dicty = {"Key1": "Val1", "Key2": "Val2"}
setunion = set()
setunion.union(dicty)
=> set(['Key2', 'Key1'])
When you attempt to add it using set.add()
you get an error:
setadd = set()
setadd.add(dicty)
Traceback (most recent call last):
File "python", line 1, in <module>
TypeError: unhashable type: 'dict'
Obviously, this behaviour is very different from lists:
listy = []
listy.append(dicty)
listy
=> [{'Key2': 'Val2', 'Key1': 'Val1'}]
In the docs it says that sets are unordered collections of hashable objects, which is a hint to some of the issues above.
Questions
What's going on here? Set items have to be hashable, so clearly that has to do with why I'm only adding the keys to the set with .union()
, but why the error with .add()
?
Is there some usability reason behind the difference in behavior of sets from lists?
Is there a datatype in Python (or a library) that essentially functions like a list, but only keeps unique items?