What are frozen sets in python?

0 votes
Are Frozen sets mutable?
Jun 11 in Python by Fata
• 320 points
14 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

A frozen set in Python is a set whose values cannot be modified. This means that it is immutable, unlike a normal set. Frozen sets help serve as a key in dictionary key-value pairs.

How to create frozen sets?

Frozen sets can be obtained using the frozenset() method. This function takes any iterable items and converts it to immutable.

Example:

1

2

3

a={1, 2,5, 4.6, 7.8, 'r', 's'}

b=frozenset(a)

print(b)

Output: frozenset({1, 2, 4.6, 5, 7.8, ‘r’, ‘s’})

The above output consists of set b which is a frozen version of set a.

Accessing Elements of a Frozen Set

Elements of a frozen set can be accessed by looping through it as follows:

Example:

1

2

3

b=frozenset([1, 2, 4.6, 5, 7.8, 'r', 's'])

for x in b:

print(x)

Output:

1
2
4.6
5
7.8
s

The above output shows that using the for loop, all elements of the frozen set b have been returned one after the other.

Frozen sets are immutable, therefore, you cannot perform operations such as add(), remove(), update(), etc.

answered Jun 11 by Nisa
• 640 points

Related Questions In Python

0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 17, 2018 in Python by Johnathon
• 9,070 points
23 views
0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14 in Python by Shashank
• 1,350 points
37 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,670 points

reshown Aug 24, 2018 by Priyaj 46 views
+5 votes
1 answer

What are metaclasses in Python?

A metaclass instantiates and defines behavior for ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 56,140 points
24 views
0 votes
1 answer

What are the ternary conditional operator in Python?

The Ternary Conditional operator was added in ...READ MORE

answered Sep 19, 2018 in Python by SDeb
• 10,780 points
37 views
0 votes
1 answer

What are namespaces in Python?

Namespace is basically a way to implement ...READ MORE

answered Oct 31, 2018 in Python by SDeb
• 10,780 points
22 views
0 votes
1 answer

what are nested if statements in python?

nested if statements are statements with more ...READ MORE

answered May 20 in Python by Mohammad
• 1,400 points
12 views
0 votes
1 answer
0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11 in Python by Nisa
• 640 points
17 views
0 votes
1 answer

How to check if a list is empty in python?

To check if a list is empty ...READ MORE

answered May 27 in Python by Nisa
• 640 points
11 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.