How to Use Frozenset Function in Python


In this example, we will learn 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'})
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement