Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use frozen set as Dict key?

It obviously works but are there cases where two sets of same elements happen to add two entries in Dict? I guess I got this condition earlier and changed my code from frozenset(...) to tuple(sorted(frozenset(...))). Can someone who knows how Dict and frozenset implementation confirm if that is required or not?

like image 645
balki Avatar asked Feb 17 '15 16:02

balki


People also ask

What Cannot be used as a key in a dictionary?

We can use integer, string, tuples as dictionary keys but cannot use list as a key of it .

What is the use of frozen set?

Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.

Why a set Cannot be used as a dictionary key but a tuple can?

A key must be immutable. You can use a tuple as a key if all of the elements contained in the tuple are immutable. (If the tuple contains mutable objects, it cannot be used as a key.) Hence a tuple to be used as a key can contain strings, numbers, and other tuples containing references to immutable objects.

How do you convert a frozen set?

Python frozenset object is an immutable unordered collection of data elements. Therefore, you cannot modify the elements of the frozenset. To convert this set into a list, you have to use the list function and pass the set as a parameter to get the list object as an output.


2 Answers

Is it safe to use a frozenset as a dict key? Yes.

According to the docs, Frozenset is hashable because it's immutable. This would imply that it can be used as the key to a dict, because the prerequisite for a key is that it is hashable.

From the FrozenSet docs

The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.

And redundantly, from the Dictionary docs:

...keys, which can be any immutable type


To clarify, a set (by definition), frozen or not, does not preserve order. They are stored internally with order not taken into account and with duplicate elements removed, so two sets built in different orders would be equivalent keys in a dictionary – they are the same.

>>> frozenset([1,2,2,3,3]) == frozenset([3,2,1,1,1])
True

and likewise,

>>> d = {}
>>> d[frozenset([1,1,2,3])] = 'hello'
>>> d[frozenset([1,2,3,3])]
'hello'
>>> d[frozenset([3,3,3,2,1,1,1])]
'hello'
>>> d[frozenset([2,1,3])]
'hello'
like image 166
Friendly King Avatar answered Oct 09 '22 10:10

Friendly King


are there cases where two sets of same elements happen to add two entries in Dict?

No. frozenset hashing algorithm doesn't depend on the order of the elements, only on elements themselves. Two FS'es with the same elements are equal and have equal hashes, thus satisfying both criteria for "dict identity", in other words, they are the same dict key:

>>> a = frozenset([1,1,1,1,2,3])
>>> b = frozenset([3,3,3,3,2,1])
>>> {a:1, b:2}
{frozenset([1, 2, 3]): 2}
like image 24
georg Avatar answered Oct 09 '22 09:10

georg