Python Frozenset


title: Python Frozenset

frozenset basic info

The frozenset type is a builtin set types which is immutable and hashable — its contents cannot be altered after it is created; however, it can be used as a dictionary key or as an element of another set. Frozensets are like sets except that they cannot be changed, i.e they are immutale.

>>> cities = frozenset(["Frankfurt", "Basel", "Freiburg"]) >>> cities.add("Strasbourg") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'frozenset' object has no attribute 'add' >>>

frozenset constructor:

frozenset([iterable])

The iterable contains elements to initialize the frozenset with. The iterable can be set, dictionary, tuple etc. If no parameter is passed, the frozenset() method returns an empty frozenset.

Examples

>>> vowels = ('a', 'e', 'i', 'o', 'u') >>> fSet = frozenset(vowels) >>> print("The frozen set is: ", fSet) The frozen set is: frozenset({'i', 'e', 'a', 'u', 'o'}) >>> print("The empty frozen set is: ", frozenset()) The empty frozen set is: frozenset() >>>

Another Example

>>> person = {"name": "John", "age": 23, "sex": "male"} >>> fSet = frozenset(person) >>> print("The frozen set is: ", fSet) The frozen set is: frozenset({'sex', 'name', 'age'}) >>>

Additional Information

Python Frozenset()

Set Types — set, frozenset

Python Tutorial: Sets and Frozen sets

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.