TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python frozenset: Immutable Sets

Use frozenset to create an immutable set. Frozensets cannot be changed once created, but can be hashed.
Frozenset. An immutable thing cannot be changed. It is frozen in place. In Python we have some immutable types—these lack features but gain others.
With frozenset, we have an immutable set. We cannot add or remove elements. In every other way it is like a set. And because it cannot be changed, it can be used as a dictionary key.
A simple example. Here is an example that uses the frozenset keyword. We create an immutable set of strings (bird, plant and fish).

Tip: A frozenset can be used where a set cannot. It can be a dictionary key or a set element itself.

Dictionary

Tip 2: We initialize a frozenset with the frozenset() built-in. We pass it a list of strings.

Python program that uses frozenset # Strings to put in frozenset. keys = ["bird", "plant", "fish"] # Create frozenset. f = frozenset(keys) print(f) # Cannot add to frozenset. try: f.add("cat") except AttributeError: print("Cannot add") # Can use frozenset as key to dictionary. d = {} d[f] = "awesome" print(d) Output frozenset({'plant', 'bird', 'fish'}) Cannot add {frozenset({'plant', 'bird', 'fish'}): 'awesome'}
Nested frozensets. Let us try to create a frozenset of frozensets. With sets, this is not possible, but frozensets can be hashed like other values.

Here: We create a frozenset that contains possible color pairs (which are stored as frozensets also).

Result: We can test the frozenset to see whether a pair of colors exists. This uses hashing.

In
Python program that uses nested frozensets # Create 3 frozensets. colors1 = frozenset(["blue", "red"]) colors2 = frozenset(["orange", "black"]) colors3 = frozenset(["black", "white"]) # Create a frozenset of two frozensets. possible = frozenset([colors1, colors2]) # This frozenset is included. if colors1 in possible: print("Possible:", colors1) # This one is not. if colors3 not in possible: print("Not possible:", colors3) Output Possible: frozenset({'blue', 'red'}) Not possible: frozenset({'white', 'black'})
Unhashable error. A set cannot be hashed. So when we try to create a set of sets we will get an "objects are unhashable" error (a TypeError).
Python program that causes unhashable error # This example will not compile. example = {1, 2, 3} example2 = {example, {5, 6, 7}} Output Traceback (most recent call last): File "C:\programs\file.py", line 6, in <module> example2 = {example, {5, 6, 7}} TypeError: 'set' objects are unhashable
Some research. The frozenset type is a built-in type. Its main purpose in Python is to provide immutability and hashing—these features sometimes lead to higher quality code.Built-ins

Quote: 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.

Built-in Types: Python.org
Here we have an immutable and hashable set. We can test a frozenset for the existence of other pairs—providing a fast way to search for multiple elements at once with hashing.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf