Python frozenset() Function – Tutorial with Examples

The frozenset() function in Python is a built-in function that creates a new frozenset object. A frozenset is a type of set that is immutable, meaning its elements cannot be changed once it is created. This makes frozenset objects ideal for use as keys in dictionaries and elements in other sets, where the elements must remain constant.

Syntax

frozenset([iterable])

Parameters

  • iterable – An optional iterable object to initialize the frozenset with. If this argument is not provided, an empty frozenset is returned.

Return Value

The frozenset() function returns a new frozenset object, initialized with the specified iterable if one is provided. If the iterable contains duplicates, the duplicates are automatically removed and only unique elements are stored in the frozenset.

Examples

Example 1: Creating a Frozenset from a List

fruits = ["apple", "banana", "cherry", "apple"]
fruits_set = frozenset(fruits)
print(fruits_set)

Output:

frozenset({'banana', 'cherry', 'apple'})

In this example, a list of fruits is created and duplicates are included. The frozenset() function is then used to create a frozenset from this list. Since frozensets only store unique elements, the duplicates are automatically removed and the frozenset only contains the unique elements of the list.

Example 2: Using a Frozenset as a Key in a Dictionary

fruits = frozenset(["apple", "banana", "cherry"])
fruit_prices = {fruits: 5.0}
print(fruit_prices)

Output:

{frozenset({'banana', 'cherry', 'apple'}): 5.0}

In this example, a frozenset is created from a list of fruits and used as a key in a dictionary. The value associated with this key is the price of the fruit. Since frozensets are immutable, they can be used as keys in dictionaries and their values can be looked up in a reliable and efficient manner.

Example 3: Using a Frozenset in a Set

fruits1 = frozenset(["apple", "banana", "cherry"])
fruits2 = frozenset(["orange", "peach", "banana"])
fruits_set = {fruits1, fruits2}
print(fruits_set)

Output:

{frozenset({'banana', 'cherry', 'apple'}), frozenset({'banana', 'peach', 'orange'})}

In this example, two frozensets are created from two lists of fruits and added to a set. Since sets only store unique elements, the frozensets are stored in the set as unique elements, even if they contain similar elements. This is a useful way to store multiple sets of unique elements, while keeping them separate and distinct.

Use Cases

  • As keys in dictionaries: Since frozensets are immutable, they can be used as keys in dictionaries, which makes it possible to store values associated with unique sets of elements.
  • As elements in other sets: Frozensets can also be used as elements in other sets, making it possible to store multiple sets of unique elements and easily perform set operations on them.
  • As members of collections: Frozensets can also be used as members of collections, making it possible to store sets of elements within lists, tuples, or other collections.

In conclusion, the frozenset() function in Python provides a useful and efficient way to create immutable sets of elements, which can be used as keys in dictionaries, elements in sets, and members of collections. It is a versatile tool that can be used in a variety of applications and is worth learning and incorporating into your Python programming skillset.

Leave a Reply

Your email address will not be published. Required fields are marked *