frozenset() Function in Python:
The frozenset() function returns an immutable frozenset object that has been initialized with elements from the iterable passed in.
A frozen set is nothing more than an immutable version of a Python set object. While elements of a set can be changed at any time, elements of a frozen set do not change after they are created.
As a result, frozen sets can be used as Dictionary keys or as elements of another set. However, it is not ordered in the same way that sets are (the elements can be set at any index).
Syntax:
frozenset(iterable)
Parameters
The frozenset() function only accepts one parameter:
iterable: This is Optional. The iterable contains the elements to be used to initialize the frozenset.
Iterable types include set, dictionary, tuple, and so on.
Return Value:
The frozenset() function returns an immutable frozenset that has been initialized with elements from the specified iterable.
It returns an empty frozenset if no parameters are passed.
Examples:
Example1:
Input:
Given List = ['good', 'morning', 'btechgeeks']
Output:
The Given list's frozenset = frozenset({'morning', 'good', 'btechgeeks'}) An empty frozenset = frozenset()
Example2:
Input:
Given dictionary = {"hello": 12, "this": 14, "is": 10, "btechgeeks": 20}
Output:
The Given dictionary's frozenset : frozenset({'btechgeeks', 'is', 'hello', 'this'})
frozenset() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the list as static input and store it in a variable.
- Pass the given list as an argument to the frozenset () function that returns an immutable frozenset object that has been initialized with elements from the iterable passed in.
- Store it in another variable.
- Print the frozenset of the given list.
- Print an empty frozenset without passing any parameters to the frozenset () function.
- The Exit of the Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = ['good', 'morning', 'btechgeeks'] # Pass the given list as an argument to the frozenset () function that returns # an immutable frozenset object that has been initialized with elements from # the iterable passed in. # Store it in another variable. rslt = frozenset(gvn_lst) # Print the frozenset of the given list. print("The Given list's frozenset =", rslt) # Print an empty frozenset without passing any parameters to the frozenset () # function. print("An empty frozenset = ", frozenset())
Output:
The Given list's frozenset = frozenset({'morning', 'good', 'btechgeeks'}) An empty frozenset = frozenset()
Similarly, do the same for the dictionaries
When you use a dictionary as an iterable for a frozen set, the set is created using only dictionary keys.
# Give the dictionary as static input and store it in a variable. gvn_dict = {"hello": 12, "this": 14, "is": 10, "btechgeeks": 20} # Pass the given dictionary as an argument to the frozenset () function that returns # an immutable frozenset object that has been initialized with elements from # the iterable passed in. # Store it in another variable. rslt = frozenset(gvn_dict) # Print the frozenset of the given dictionary. print("The Given dictionary's frozenset :") print(rslt)
Output:
The Given dictionary's frozenset : frozenset({'btechgeeks', 'is', 'hello', 'this'})
Operations of frozenset
frozenset, like normal sets, can perform operations such as copy, difference, intersection, symmetric difference, and union.
# Give two frozenset's and store them in two separate variables gvn_froznset1 = frozenset([10, 20, 30, 40]) gvn_froznset2 = frozenset([30, 40, 50, 60]) # copy the given frozenset1 to new variable. neww = gvn_froznset1.copy() print(neww) # Union of frozenset1 and frozenset2 print(gvn_froznset1.union(gvn_froznset2)) # intersection of frozenset1 and frozenset2 print(gvn_froznset1.intersection(gvn_froznset2)) # difference of frozenset1 and frozenset2 print(gvn_froznset1.difference(gvn_froznset2)) # symmetric_difference of frozenset1 and frozenset2 print(gvn_froznset1.symmetric_difference(gvn_froznset2))
Output:
frozenset({40, 10, 20, 30}) frozenset({40, 10, 50, 20, 60, 30}) frozenset({40, 30}) frozenset({10, 20}) frozenset({10, 50, 20, 60})
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Pass the given list as an argument to the frozenset () function that returns an immutable frozenset object that has been initialized with elements from the iterable passed in.
- Store it in another variable.
- Print the frozenset of the given list.
- Print an empty frozenset without passing any parameters to the frozenset () function.
- The Exit of the Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Pass the given list as an argument to the frozenset () function that returns # an immutable frozenset object that has been initialized with elements from # the iterable passed in. # Store it in another variable. rslt = frozenset(gvn_lst) # Print the frozenset of the given list. print("The Given list's frozenset =", rslt) # Print an empty frozenset without passing any parameters to the frozenset () # function. print("An empty frozenset = ", frozenset())
Output:
Enter some random List Elements separated by spaces = 10 40 70 90 The Given list's frozenset = frozenset({40, 10, 90, 70}) An empty frozenset = frozenset()