Frozen Set in Python
- Get link
- X
- Other Apps
Frozen Set in Python
In Python, a set is an unordered collection of unique elements, where each element is immutable. However, there is another type of set called a "frozen set," which is an immutable version of a set.
A frozen set in Python is similar to a set, with the only difference being that the elements of a frozen set cannot be modified once it is created. This means that you can add or remove elements from a set, but you cannot do the same with a frozen set.
Creating a Frozen Set
You can create a frozen set in Python using the frozenset()
function. Here's an example:
# creating a set my_set = {1, 2, 3, 4} # creating a frozen set my_frozen_set = frozenset(my_set)
In the above example, we first create a regular set my_set
containing the elements {1, 2, 3, 4}
. We then create a frozen set my_frozen_set
by passing my_set
to the frozenset()
function.
Accessing Elements of a Frozen Set
Since a frozen set is an immutable object, you cannot add or remove elements from it. However, you can access its elements using a for
loop or by converting it to a list.
# creating a frozen set my_frozen_set = frozenset([1, 2, 3, 4]) # iterating over the elements for element in my_frozen_set: print(element) # converting to a list my_list = list(my_frozen_set) print(my_list) # output: [1, 2, 3, 4]
In the above example, we first create a frozen set my_frozen_set
containing the elements {1, 2, 3, 4}
. We then iterate over the elements of the frozen set using a for
loop and print them. Finally, we convert the frozen set to a list my_list
using the list()
function and print it.
Frozen Set Operations
Like regular sets, frozen sets support various set operations such as union, intersection, difference, and symmetric difference. However, since frozen sets are immutable, these operations return new frozen sets instead of modifying the existing one.
# creating two frozen sets set1 = frozenset([1, 2, 3]) set2 = frozenset([2, 3, 4]) # performing set operations union_set = set1.union(set2) intersection_set = set1.intersection(set2) difference_set = set1.difference(set2) symmetric_difference_set = set1.symmetric_difference(set2) # printing the results print(union_set) # output: frozenset({1, 2, 3, 4}) print(intersection_set) # output: frozenset({2, 3}) print(difference_set) # output: frozenset({1}) print(symmetric_difference_set) # output: frozenset({1, 4})
In the above example, we create two frozen sets set1
and set2
. We then perform various set operations using the respective methods and print the resulting frozen sets.
Conclusion
Frozen sets in Python are a useful data type when you need an immutable set of elements. Since frozen sets are immutable, they are useful when you need to use sets as keys in dictionaries or when you need to store sets in a tuple. By using frozen sets, you can perform set operations and access the elements of the set without worrying about accidentally modifying them.
Happy Learning!! Happy Coding!!
- Get link
- X
- Other Apps
Comments