Python frozenset() - Create Immutable Set

Updated on November 22, 2024
frozenset() header image

Introduction

The frozenset() function in Python allows you to create an immutable version of a set, known as a frozenset. Unlike regular sets, frozensets are static and their elements cannot be changed once assigned. This makes frozensets useful in situations where a collection of unique items is needed and must not be altered, such as in key sets for dictionaries or as elements of other sets.

In this article, you will learn how to utilize the frozenset() function to create and work with immutable sets. This includes methods for initializing frozensets, operations you can perform with them, and their practical usage in comparing sets and using them as keys in dictionaries.

Creating and Initializing Frozensets

Create an Empty Frozenset

  1. Initialize an empty frozenset using frozenset() without any arguments.

    python
    empty_fset = frozenset()
    print(empty_fset)
    

    This code initializes an empty frozenset. The output will be frozenset() indicating that the set is empty.

Create a Frozenset from a List

  1. Convert a list to a frozenset using frozenset() with the list as an argument.

    python
    list_data = [1, 2, 3, 2, 1]
    list_fset = frozenset(list_data)
    print(list_fset)
    

    Here, frozenset() takes a list and converts it into a frozenset, automatically removing any duplicate items. The output will be frozenset({1, 2, 3}).

Create a Frozenset from a Tuple

  1. Use a tuple to form a frozenset.

    python
    tuple_data = (1, 2, 3, 4, 2, 1)
    tuple_fset = frozenset(tuple_data)
    print(tuple_fset)
    

    The tuple converted into a frozenset will retain unique elements and ignore duplicates. The output would be frozenset({1, 2, 3, 4}).

Operations with Frozensets

Performing Union

  1. Use the union() method to combine frozensets.

    python
    set1 = frozenset([1, 2, 3])
    set2 = frozenset([3, 4, 5])
    union_set = set1.union(set2)
    print(union_set)
    

    This code combines set1 and set2 into a new frozenset containing all unique elements from both. The result is frozenset({1, 2, 3, 4, 5}).

Finding Intersections

  1. Obtain the common elements between two frozensets using the intersection() method.

    python
    set1 = frozenset([1, 2, 3])
    set2 = frozenset([3, 4, 5])
    intersection_set = set1.intersection(set2)
    print(intersection_set)
    

    The intersection() method identifies elements present in both set1 and set2. Here, it will output frozenset({3}).

Difference between Frozensets

  1. Discover elements present in one frozenset and not in the other using the difference() method.

    python
    set1 = frozenset([1, 2, 3])
    set2 = frozenset([3, 4, 5])
    difference_set = set1.difference(set2)
    print(difference_set)
    

    This method returns elements exclusive to set1 when compared with set2. The output will be frozenset({1, 2}).

Conclusion

Utilizing the frozenset() function offers a robust way to handle immutable sets in Python. These sets are essential when you need a collection of items that must not change over time, such as when using sets as keys in dictionaries. The operations like union, intersection, and difference broaden the utility of frozensets in various applications, enhancing both functionality and data safety in complex programs. Implement these techniques to ensure your sets remain unmodified while still being able to perform vital set operations.