No, you canβt. You can add immutable values ββto the set. This limitation is not only related to the ability to compare values; you need to check both for equality and for obtaining a hash value, and most of all the value should remain stable. Variable values ββdo not meet the latest requirement.
The dictionary can be made unchanged by turning it into a series of tuples with a key; if the values ββare immutable, the following works:
widget_set = {tuple(sorted(widget.items()))}
This makes it possible to check for the presence of the same dictionary, at least for testing tuple(sorted(somedict.items())) in widget_set . Returning values ββback to dict is a call to dict on it:
dict(widget_set.pop())
Demo:
>>> widget = { ... 'lunch': 'eggs', ... 'dunner': 'steak' ... } >>> widget_set = {tuple(sorted(widget.items()))} >>> tuple(sorted(widget.items())) in widget_set True >>> dict(widget_set.pop()) {'lunch': 'eggs', 'dunner': 'steak'}
source share