{"id":25069,"date":"2021-11-10T09:37:49","date_gmt":"2021-11-10T04:07:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=25069"},"modified":"2021-11-10T09:37:49","modified_gmt":"2021-11-10T04:07:49","slug":"python-set-issubset-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-issubset-method-with-examples\/","title":{"rendered":"Python Set issubset() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set issubset() Method in Python:<\/strong><\/p>\n If all of the items in the set exist in the specified set, the issubset() method returns true; otherwise, it returns False.<\/p>\n For Example,<\/p>\n Let p = {1, 2, 3}<\/p>\n q= {1, 2, 3, 4, 5, 6}<\/p>\n Here all the elements of set ‘p ‘ are in the set ‘q’ .Hence set p<\/strong> is a subset of set q<\/strong>.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set:<\/strong> This is\u00a0 Required. It is the set to look for items that are similar.<\/p>\n Return Value:<\/strong><\/p>\n issubset() produces a result.<\/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 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":" Prerequisite: Python set() Function with Examples Set issubset() Method in Python: If all of the items in the set exist in the specified set, the issubset() method returns true; otherwise, it returns False. For Example, Let p = {1, 2, 3} q= {1, 2, 3, 4, 5, 6} Here all the elements of set ‘p …<\/p>\nset.issubset(set)<\/pre>\n
\n
Given first set = {20, 30, 100}\r\nGiven second set = {100, 20, 80, 70, 30}<\/pre>\n
Check if the given first set is subset of the second set: True<\/pre>\n
Given first set = {3, 5, 6, 1}\r\nGiven second set = {3, 7, 8 ,9}<\/pre>\n
Check if the given first set is subset of the second set: False<\/pre>\n
Set issubset() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first set as static input and store it in a variable.\r\nfst_set = {20, 30, 100}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {100, 20, 80, 70, 30}\r\n# Apply the issubset() method to the given first and second sets to check if the\r\n# given first set is subset of the second set or not.\r\n# Store it in another variable.\r\nrslt = fst_set.issubset(scnd_set)\r\n# Print the above result.\r\nprint(\"Check if the given first set is subset of the second set:\", rslt)\r\n<\/pre>\n
Check if the given first set is subset of the second set: True<\/pre>\n
Similarly, check for the other example<\/h5>\n
fst_set = {'a', 'l', 'm'}\r\nscnd_set = {'m', 'a', 'l'}\r\nrslt = fst_set.issubset(scnd_set)\r\nprint(\"Check if the given first set is subset of the second set:\", rslt)\r\n<\/pre>\n
Check if the given first set is subset of the second set: True<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first set as user input using set(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nfst_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n# Give the second set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nscnd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n\r\n# Apply the issubset() method to the given first and second sets to check if the\r\n# given first set is subset of the second set or not.\r\n# Store it in another variable.\r\nrslt = fst_set.issubset(scnd_set)\r\n# Print the above result.\r\nprint(\"Check if the given first set is subset of the second set:\", rslt)\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 3 5 6 1\r\nEnter some random Set Elements separated by spaces = 3 7 8 9\r\nCheck if the given first set is subset of the second set: False<\/pre>\n