Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll introduce you to frozensets in Python. A frozenset is an immutable, unordered collection of unique elements. It is similar to a set, but since it is immutable, it can be used as a key in a dictionary or an element in a set.
1. Creating a Frozenset
You can create a frozenset using the frozenset()
constructor. The constructor takes an iterable as an argument, such as a list, tuple, set, or string.
# Creating a frozenset from a list frozen_list = frozenset([1, 2, 3, 4, 5]) # Creating a frozenset from a tuple frozen_tuple = frozenset((1, 2, 3, 4, 5)) # Creating a frozenset from a set my_set = {1, 2, 3, 4, 5} frozen_set = frozenset(my_set) # Creating a frozenset from a string frozen_string = frozenset("hello") print(frozen_list) print(frozen_tuple) print(frozen_set) print(frozen_string)
2. Frozenset Operations
Frozensets support various set operations, such as union, intersection, difference, and symmetric difference. However, since frozensets are immutable, you cannot add or remove elements after they are created.
frozen_a = frozenset([1, 2, 3]) frozen_b = frozenset([3, 4, 5]) union_frozen = frozen_a | frozen_b print(union_frozen) # Output: frozenset({1, 2, 3, 4, 5})
intersection_frozen = frozen_a & frozen_b print(intersection_frozen) # Output: frozenset({3})
difference_frozen = frozen_a - frozen_b print(difference_frozen) # Output: frozenset({1, 2})
symmetric_difference_frozen = frozen_a ^ frozen_b print(symmetric_difference_frozen) # Output: frozenset({1, 2, 4, 5})
3. Frozenset Membership
You can check if an element is a member of a frozenset using the in
keyword.
frozen_set = frozenset([1, 2, 3, 4, 5]) print(1 in frozen_set) # Output: True print(6 in frozen_set) # Output: False
4. Frozenset Length
You can find the number of elements in a frozenset using the len()
function.
frozen_set = frozenset([1, 2, 3, 4, 5]) print(len(frozen_set)) # Output: 5
How to Create a Frozenset in Python:
frozenset()
constructor to create an immutable frozenset.# Example my_frozenset = frozenset([1, 2, 3])
Frozenset Data Type in Python:
# Example my_frozenset = frozenset([1, 2, 3])
Python Frozenset Methods and Operations:
union()
, intersection()
, difference()
, etc.# Example set1 = frozenset([1, 2, 3]) set2 = frozenset([2, 3, 4]) union_set = set1.union(set2)
Accessing Elements in a Frozenset in Python:
# Example for element in my_frozenset: print(element)
Iterating Through a Frozenset in Python:
# Example for element in my_frozenset: print(element)
Differences Between Set and Frozenset in Python:
# Example my_set = {1, 2, 3} my_frozenset = frozenset(my_set)
Using Frozensets for Immutability in Python:
# Example my_set = {1, 2, 3} my_frozenset = frozenset(my_set)
Converting Set to Frozenset in Python:
frozenset()
constructor.# Example my_set = {1, 2, 3} my_frozenset = frozenset(my_set)
Common Operations on Python Frozensets:
union()
, intersection()
, difference()
, and more.# Example set1 = frozenset([1, 2, 3]) set2 = frozenset([2, 3, 4]) union_set = set1.union(set2)