Javaexercise.com

Python Frozen Set

Python Frozen Set

Python is a high level, interpreted programming language, designed and developed by Guido van Rossum in the 1990s. It is known for its simplicity and easy to understand capability. It is one of the most popular programming languages present in the software industry. One of the most important concepts in python is that of Frozen Sets. Let us have a look at it in detail.

What are Frozen Sets?

In python, iterables are data structures used to store multiple data items of same or different data types which need to be iterated over using structures like loops. Some iterables in python such as tuples are not changeable. This means that once the iterable is created, you are not allowed to change it or any of the data items within it. This property is extremely helpful when you have multiple programmers working on a very large project with long code and you want to make sure that a particular data structure is not accidentally updated at a later stage. Other iterables in python such as lists, dictionaries, etc are changeable, that is, they allow you to make updates to the data structure and the items present inside it. Frozen Sets are useful when you want to make these data structures unchangeable but do not wish to go through the hassle of converting them to tuples. As the name suggests, Frozen set is a frozen or unchangeable version of sets in python. Like sets, frozen sets are also not ordered

Syntax

The syntax for creating Frozen Sets is pretty straightforward and is as follows. We simply use the frozenset() function in python along with the desired iterable that needs to be made immutable as a parameter to this function. It returns a frozenset type of object which can then be assigned to a desired variable as well - 

<variable_name> = frozenset(<iterable>)

Creating a frozen set using a python list

In python, you can create a frozen set using any form of iterable data structure. A list is such a structure that is inherently mutable but can be made immutable on using a frozen set structure. We have a list named a containing three data items, 0, 1 and 2. To make a frozen set data structure from the list, we use the frozenset() function and pass a as the parameter to this function. The returned frozenset object is stored in a variable named froz and it is later printed out. Let us have a look at the python code and output for this example - 

# Defining a list

a = [0, 1, 2]

# Creating a frozen set

froz = frozenset(a)

# Printing

print(froz)

Output

frozenset({0, 1, 2})

Creating a frozen set using a python dictionary

In python, you can create a frozen set using any form of iterable data structure. A dictionary is such a structure that is inherently mutable but can be made immutable on using a frozen set structure. We have a dictionary named a containing three data items, in the form of key-value pairs. The keys are x, y and z and corresponding values are 0, 1 and 2. To make a frozen set data structure from the dictionary, we use the frozenset() function and pass the variable name, a as the parameter to this function. The returned frozenset object is stored in a variable named froz and it is later printed out. One important thing to note here is that the resulting frozen set contains only the keys of the dictionary i.e. x, y and z. Let us have a look at the python code and output for this example - 

# Defining a dictionary

a = {'x':0, 'y':1, 'z':2}

# Creating a frozen set

froz = frozenset(a)

# Printing

print(froz)

Output

frozenset({'x', 'y', 'z'})

Frozen sets are not changeable

As mentioned earlier, frozen sets are specifically created in python to make the desired iterables immutable or unchangeable. So obviously when you try to change any data item in a frozen set, python would immediately throw an error. Here we have a frozen set froz of a list containing 0, 1 and 2, similar to the example used previously. If we try to change the second data item from 1 to 5, python will not allow it and throw an error. Remember that indices in python are zero-based so to access the second data item, we shall use the index 1. Let us take a look at the python code and corresponding output for this method - 

# Defining a list

a = [0, 1, 2]

# Creating a frozen set

froz = frozenset(a)

# Trying to change a data item

froz[1] = 5

Output

TypeError: 'frozenset' object does not support item assignment

Conclusion

In this topic, we have learned the use and advantages of frozen sets in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.