What are frozen sets in python

0 votes
Are Frozen sets mutable?
Jun 11, 2019 in Python by Fata
• 1,050 points
2,908 views

1 answer to this question.

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, 2019 by Nisa
• 1,090 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 18, 2018 in Python by Johnathon
• 9,090 points
859 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, 2019 in Python by Shashank
• 1,370 points
1,023 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,710 points

reshown Aug 24, 2018 by Priyaj 1,355 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
• 58,020 points
861 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
• 13,300 points
730 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
• 13,300 points
705 views
0 votes
1 answer

what are nested if statements in python?

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

answered May 21, 2019 in Python by Mohammad
• 3,230 points
785 views
0 votes
1 answer

What are the naming conventions for variables and data types in python?

There are certain rules that we have ...READ MORE

answered May 21, 2019 in Python by Mohammad
• 3,230 points
2,081 views
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, 2019 in Python by Nisa
• 1,090 points
1,709 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, 2019 in Python by Nisa
• 1,090 points
1,055 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP