The frozenset() function returns an immutable frozenset object which is like an immutable set object.
Example
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
List1 = ['1', 'A', '2','B','C','D']
x = frozenset(List1)
print(List1)
x[1] = "strawberry"
Output:
Traceback (most recent call last):
TypeError: 'frozenset' object does not support item assignment
Parameters
Name |
Description |
object |
An iterable object, like list, set, tuple, etc. |
Return Value
It returns an immutable frozenset object.
In this example we will show how to use Frozenset function in Python.
Syntax
class frozenset([iterable]);
Here iterable is optional, it refers to an iterable object such as a list, a dictionary, a tuple, and so on.
Source Code
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Generate a new immutable collection
a = frozenset(range(10))
print(a)
# tuple
letters = ('a', 'b', 'c', 'd', 'e')
fSet = frozenset(letters)
print('The frozen set is:', fSet)
# dictionary
cars = {"Toyota": "Japan", "Ford": "USA", "Volkswagen": "German"}
fSet = frozenset(cars)
print('The frozen set is:', fSet)
Output:
frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
The frozen set is: frozenset({'a', 'c', 'b', 'e', 'd'})
The frozen set is: frozenset({'Volkswagen', 'Toyota', 'Ford'})