Re:Python overtakes JavaScript as most questioned language o...
by iainmerrick on Saturday April 20, 2019 @07:56AM
(#19706053)
Not often, but when you do want to do it, it’s frustrating (and unusual for Python) that the natural and obvious way doesn’t work.
[ Parent
| on HN ]
Re:Python overtakes JavaScript as most questioned language o...
by jotaf on Saturday April 20, 2019 @08:21AM
(#19706146)
When I read this my first thought was "you should use a FrozenDict", but looking into it, it seems that it doesn't exist; I assumed so because of FrozenSets (hashable sets).
You can still create a hashable key from a dict with FrozenSet(mydict.items()).
Re:Python overtakes JavaScript as most questioned language o...
by np_tedious on Saturday April 20, 2019 @08:24AM
(#19706159)
Ah nice. Yes that last suggestion is probably a more simple and elegant approach than anything I'd mentioned / thought about
Re:Python overtakes JavaScript as most questioned language o...
by typon on Saturday April 20, 2019 @08:49AM
(#19706254)
You don't really need a FrozenSet if you're already willing to iterate over this dictionary. You can just use a tuple, which is also hashable.
tuple(d.items())
Re:Python overtakes JavaScript as most questioned language o...
by detaro on Saturday April 20, 2019 @08:50AM
(#19706260)
That depends on the order in which the values are are returned by items(), which you likely do not want.
>>> tuple({1:2,3:4}.items()) == tuple({3:4,1:2}.items())
False
>>> frozenset({1:2,3:4}.items()) == frozenset({3:4,1:2}.items())
True