{"id":24874,"date":"2021-11-10T09:36:39","date_gmt":"2021-11-10T04:06:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=24874"},"modified":"2021-11-10T09:36:39","modified_gmt":"2021-11-10T04:06:39","slug":"python-frozenset-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-frozenset-function-with-examples\/","title":{"rendered":"Python frozenset() Function with Examples"},"content":{"rendered":"
frozenset() Function in Python:<\/strong><\/p>\n The frozenset() function returns an immutable frozenset object that has been initialized with elements from the iterable passed in.<\/p>\n A frozen set is nothing more than an immutable version of a Python set object. While elements of a set can be changed at any time, elements of a frozen set do not change after they are created.<\/p>\n As a result, frozen sets can be used as Dictionary keys or as elements of another set. However, it is not ordered in the same way that sets are (the elements can be set at any index).<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n The frozenset() function only accepts one parameter:<\/p>\n iterable:<\/strong> This is Optional. The iterable contains the elements to be used to initialize the frozenset.<\/p>\n Iterable types include set, dictionary, tuple, and so on.<\/p>\n Return Value:<\/strong><\/p>\n The frozenset() function returns an immutable frozenset that has been initialized with elements from the specified iterable.<\/p>\n It returns an empty frozenset if no parameters are passed.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n When you use a dictionary as an iterable for a frozen set, the set is created using only dictionary keys.<\/p>\n Output:<\/strong><\/p>\n frozenset, like normal sets, can perform operations such as copy, difference, intersection, symmetric difference, and union.<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" frozenset() Function in Python: The frozenset() function returns an immutable frozenset object that has been initialized with elements from the iterable passed in. A frozen set is nothing more than an immutable version of a Python set object. While elements of a set can be changed at any time, elements of a frozen set do …<\/p>\nfrozenset(iterable)<\/pre>\n
Given List = ['good', 'morning', 'btechgeeks']<\/pre>\n
The Given list's frozenset = frozenset({'morning', 'good', 'btechgeeks'})\r\nAn empty frozenset = frozenset()<\/pre>\n
Given dictionary = {\"hello\": 12, \"this\": 14, \"is\": 10, \"btechgeeks\": 20}<\/pre>\n
The Given dictionary's frozenset :\r\nfrozenset({'btechgeeks', 'is', 'hello', 'this'})<\/pre>\n
frozenset() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['good', 'morning', 'btechgeeks']\r\n# Pass the given list as an argument to the frozenset () function that returns\r\n# an immutable frozenset object that has been initialized with elements from\r\n# the iterable passed in.\r\n# Store it in another variable.\r\nrslt = frozenset(gvn_lst)\r\n# Print the frozenset of the given list.\r\nprint(\"The Given list's frozenset =\", rslt)\r\n# Print an empty frozenset without passing any parameters to the frozenset ()\r\n# function.\r\nprint(\"An empty frozenset = \", frozenset())\r\n<\/pre>\n
The Given list's frozenset = frozenset({'morning', 'good', 'btechgeeks'})\r\nAn empty frozenset = frozenset()<\/pre>\n
Similarly, do the same for the dictionaries<\/h5>\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {\"hello\": 12, \"this\": 14, \"is\": 10, \"btechgeeks\": 20}\r\n# Pass the given dictionary as an argument to the frozenset () function that returns\r\n# an immutable frozenset object that has been initialized with elements from\r\n# the iterable passed in.\r\n# Store it in another variable.\r\nrslt = frozenset(gvn_dict)\r\n# Print the frozenset of the given dictionary.\r\nprint(\"The Given dictionary's frozenset :\")\r\nprint(rslt)\r\n<\/pre>\n
The Given dictionary's frozenset :\r\nfrozenset({'btechgeeks', 'is', 'hello', 'this'})<\/pre>\n
Operations of frozenset<\/h5>\n
# Give two frozenset's and store them in two separate variables\r\ngvn_froznset1 = frozenset([10, 20, 30, 40])\r\ngvn_froznset2 = frozenset([30, 40, 50, 60])\r\n\r\n# copy the given frozenset1 to new variable.\r\nneww = gvn_froznset1.copy()\r\nprint(neww)\r\n\r\n# Union of frozenset1 and frozenset2\r\nprint(gvn_froznset1.union(gvn_froznset2))\r\n\r\n# intersection of frozenset1 and frozenset2\r\nprint(gvn_froznset1.intersection(gvn_froznset2))\r\n\r\n# difference of frozenset1 and frozenset2\r\nprint(gvn_froznset1.difference(gvn_froznset2))\r\n\r\n# symmetric_difference of frozenset1 and frozenset2\r\nprint(gvn_froznset1.symmetric_difference(gvn_froznset2))\r\n<\/pre>\n
frozenset({40, 10, 20, 30})\r\nfrozenset({40, 10, 50, 20, 60, 30})\r\nfrozenset({40, 30})\r\nfrozenset({10, 20})\r\nfrozenset({10, 50, 20, 60})<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the frozenset () function that returns\r\n# an immutable frozenset object that has been initialized with elements from\r\n# the iterable passed in.\r\n# Store it in another variable.\r\nrslt = frozenset(gvn_lst)\r\n# Print the frozenset of the given list.\r\nprint(\"The Given list's frozenset =\", rslt)\r\n# Print an empty frozenset without passing any parameters to the frozenset ()\r\n# function.\r\nprint(\"An empty frozenset = \", frozenset())\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 40 70 90\r\nThe Given list's frozenset = frozenset({40, 10, 90, 70})\r\nAn empty frozenset = frozenset()<\/pre>\n