Menu Close

Python frozenset Function

Python frozenset function

In this article, you will learn everything about the Python frozenset() function to create an unchangeable set. frozen set in Python in Python is an immutable object which means we can’t change it once it has been created.

Stay tuned to this article, In this article, we will see all about frozen sets in Python, and also we will see how to convert any existing iterable objects into the frozen set using frozenset() function.

Python frozenset() Function

Python frozenset() function is a built-in function that is used to create and return unchangeable frozen set objects just like Python set. Probably, you would have known about Python set, In the Python set, we could have changed the set once it’s created but here it is not possible because Python frozen set is an immutable or unchangeable object.

Syntax

The Syntax of frozenset function in Python is:-

frozenset(iterable)

Parameter

Python frozenset method accepts one parameter:-

  • iterable:- An iterable object like a list, set, tuple, etc.

Return Value

frozenset function in Python unchangeable object.

Python frozenset examples

Here, In this section, we will see Python frozen set method to create a new frozen set and will see how to convert existing iterable objects like set, list, and tuple into the frozen set.

Example: Converting Python list to frozenset


mylist = ['Python', 'Java', 'C++', 'PHP', 'HTML']
result = frozenset(mylist)
print(result)

Output

frozenset({'Java', 'HTML', 'C++', 'PHP', 'Python'})

Example: Converting tuple to frozen set

students_name = ("Vishvajit", "Vikas", "Vinod", "Akash", "Veronika")
frozen_set = frozenset(students_name)
print(frozen_set)

Output

frozenset({'Vikas', 'Vishvajit', 'Akash', 'Veronika', 'Vinod'})

Example: Converting string to frozen set

string = 'Python'
frozen_set = frozenset(string)
print(frozen_set)

Output

frozenset({'P', 't', 'h', 'n', 'o', 'y'})

Example: Converting set to frozen set

programming_languages = {"Python", "Java", "PHP", "Golang", "Carbon"}
frozen_set = frozenset(programming_languages)
print(frozen_set)

Output

frozenset({'Java', 'PHP', 'Carbon', 'Python', 'Golang'})

Let’s see, What happens, If you try to change your frozen set item?

Example: We will get an error if we will try to change the frozen set


mylist = ['Python', 'Java', 'C++', 'PHP', 'HTML']
result = frozenset(mylist)
result[1] = 'CSS'
print(result)

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 3, in <module>
    result[1] = 'CSS'
TypeError: 'frozenset' object does not support item assignment

When you try to change your frozen set item, you will get an error.

Note:- You will have to remember one thing here, The order of the items can be changed in your case, If you are using all these examples because Python frozen set is an unordered object.

Conclusion

I think you will have understood all about the Python frozenset() function. I will highly recommend you if you want to create unchangeably iterable which will not change in the future then you can go with this Python frozen set concept.

If you found this article helpful, please share it. Please keep visiting for further python built-in functions tutorials.

Thanks for your valuable time… ❤️❤️🙏🙏

Other Python built-in functions


For more information:- Click Here

Python format() Function
Python exec() Function

Related Posts