Signup/Sign In
LAST UPDATED: MAY 2, 2023

frozenset in Python with Examples

Technology #python#frozenset

    What is Frozenset in Python?

    The word frozenset indicates that a Python set has been frozen. This means the elements of a set which is frozen can't be changed, they are immutable. A frozenset in Python is essentially the unchangeable or immutable version of the set object.

    Syntax of frozenset

    Following is the syntax for a frozenset:

    class frozenset([iterable])
    

    It either returns a new set which is an frozenset object that contains elements from the iterable.

    This iterable isn't always necessary. The output of the function depends on whether the parameter has been provided. The iterable could be a set, tuple, or dictionary structure. If the iterable hasn't been provided, it returns an empty set.

    What is the use of such a data structure?

    Since a frozenset is a set that is immutable it is suitable for use cases where we have to create a group of keys or some identifiers that we don't want the user to change. In such a case we can store these in the form of a frozenset hence not allowing anyone to change them. One such use case is the keys of a dictionary, if the key plays a functional role in the application to get data from a dictionary object we can make the keys as a frozenset.


    Creating a frozenset

    In the code below, we have created a tuple can with different data values. It is then passed as a parameter to the frozenset function. It converts the tuple into a frozen set and returns a frozenset with data in an arbitrary order.

    a_tuple = ('a', 'e', 'i', 'o', 'u', 1,2,3)
    my_new_fSet = frozenset(a_tuple)
    print(my_new_fSet)

    Output:

    ({'o', 1, 2, 3, 'u', 'e', 'i', 'a'})


    Creating an empty frozenset in Python

    If no iterable is passed to the frozenset() function, then the frozenset function returns an empty frozenset.

    print(frozenset())

    Output:

    frozenset()
    


    How to use frozenset as dictionary keys?

    Below we have a simple program to demonstrate how we can use a frozenset in Python as dictionary keys:

    my_first_dictionary = {"Website": "Studytonight", "Letters": 12}
    fSet = frozenset(my_first_dictionary)
    print(fSet)

    Output:

    frozenset({'Website', 'Letters'})

    We have simply provided the dictionary object to the frozenset function because by default the dictionary object returns a list of its keys.


    What happens if I try to change the frozenset?

    Once a frozenset object has been created, it can't be assigned any new value or we cannot change any existing value stored in it. If you will try to do so, python gives a TypeError. Below is a demonstration of the same:

    my_first_set = {1, 2, 3, 'a'}
    my_second_set = frozenset(my_first_set)
    # assigning/updating a value
    my_second_set[1] = 'b'

    Output:

    TypeError: 'frozenset' object does not support item assignment

    Can a set and a frozenset be compared?

    Yes, absolutely! They can be compared since the instance of a set and frozenset are compared based on the elements present inside these objects. Here is a demonstration:

    set('Studytonight') == frozenset('Studytonight')

    Output:

    True

    Can two frozensets in Python be compared?

    Yes, absolutely. They can be compared to check for equality, for being subsets of each other, to check the disjoint nature of 2 sets, or for being a superset of one other.

    Two sets are equal when one set is a subset of the other, i.e. when every element from one set is present in the other set. See the example below:

    set_two = (1,2,3,4, 'Five', 5)
    set_two = frozenset(set_two)
    set_one = (1,2,3,4, 'Five', 5)
    set_one = frozenset(set_one)
    print(set_two == set_one)

    Output:

    True

    A set is less than another set, but not equal to the other set if all the elements of the first set are a part of the second set. Below is an example.

    set_one = (1,2,3,4, 'Five', 5)
    set_one = frozenset(set_one)
    set_two = (1,2,3,4, 'One','Two','Three','Four','Five', 5)
    set_two = frozenset(set_two)
    print(set_one < set_two) # can be set_one<=set_two

    Output:

    True

    A set is greater than the other set if all elements of the second set are a part of the first set. Below is an example:

    set_two = (1,2,3,4, 'Five', 5)
    set_two = frozenset(set_two)
    set_one = (1,2,3,4, 'One','Two','Three','Four','Five', 5)
    set_one = frozenset(set_one)
    print(set_one > set_two)  #can be set_one>=set_two

    Output:

    True
    

    Operations that can't be performed on frozenset, but can be performed on a set:

    A few operations can't be performed on frozenset due to the nature of it being immutable. Below are such operations:

    1. update

    2. intersection_update

    3. difference_update

    4. symmetric_difference_update

    5. add

    6. remove

    7. discard

    8. pop

    9. clear

    For intersection_update, difference_update and symmetric_difference_update to work, an iterable needs to be passed to them as a parameter. All these functions are Python set functions and you can find how they work in our Python set tutorial.


    Conclusion

    In this post, we understood what a frozenset in Python means, its significance, usage, and the various different operations which could potentially be performed on it.

    Frequently Asked Questions(FAQs)

    1. What is difference between set and Frozenset in Python?

    The main difference between a set and a frozenset in Python is that a set is mutable, meaning its elements can be added, removed, or modified, while a frozenset is immutable, meaning its elements cannot be changed after creation.

    2. What is Frozenset used for?

    Frozenset is used to represent an immutable set of unique elements in Python.

    3. What is the difference between a tuple and a Frozenset in Python?

    A tuple is also immutable in Python, but it can contain duplicate elements and it is ordered, while a frozenset is an unordered collection of unique elements.

    4. How do you find the Frozenset in Python?

    You can create a frozenset in Python by passing an iterable (e.g. list, tuple, set) to the built-in frozenset() function. For example, my_frozenset = frozenset([1, 2, 3]).

    You may also like:

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS