{"id":25046,"date":"2021-11-10T09:37:18","date_gmt":"2021-11-10T04:07:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=25046"},"modified":"2021-11-10T09:37:18","modified_gmt":"2021-11-10T04:07:18","slug":"python-set-difference-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-difference-method-with-examples\/","title":{"rendered":"Python Set difference() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set difference() Method in Python:<\/strong><\/p>\n The difference() method returns a set containing the difference of two sets.<\/p>\n The returned set contains items that only exist in the first set and not in both.<\/p>\n For Example:<\/strong><\/p>\n If P and Q are two distinct sets. A set difference between P and Q is a set of elements that exist only in the set P but not in the set Q.<\/p>\n Let P={4,5,6,7}<\/p>\n Q={5,6,8,9}<\/p>\n P-Q={4,7}<\/p>\n Q-P ={8,9}<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set:<\/strong> This is Required. The set used to look for differences.<\/p>\n Return Value:<\/strong><\/p>\n difference() computes the difference between two sets, each of which is a set. It makes no changes to the original sets.<\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Prerequisite: Python set() Function with Examples Set difference() Method in Python: The difference() method returns a set containing the difference of two sets. The returned set contains items that only exist in the first set and not in both. For Example: If P and Q are two distinct sets. A set difference between P and …<\/p>\nset.difference(set)<\/pre>\n
Given first set = {10, 11, 12, 13}\r\nGiven second set = {11, 12, 20, 14}<\/pre>\n
firstset-secondset : {10, 13}\r\nsecondset-firstset : {20, 14}<\/pre>\n
Given first set = {20, 30, 40, 20, 30, 50, 60}\r\nGiven second set = {40, 50, 60, 100, 80}<\/pre>\n
firstset-secondset : {20, 30}\r\nsecondset-firstset : {80, 100}<\/pre>\n
Set difference() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Set Difference Using difference() method<\/h5>\n
\n
# Give the first set as static input and store it in a variable.\r\nfst_Set = {10, 11, 12, 13}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {11, 12, 20, 14}\r\n# Get the difference between the( first set - second set ) using the difference()\r\n# method.\r\n# Store it in another variable.\r\nrslt1 = fst_Set.difference(scnd_set)\r\n# Get the difference between the (second set - first set ) using the difference()\r\n# method.\r\n# Store it in another variable.\r\nrslt2 = scnd_set.difference(fst_Set)\r\n# Print the result (first set - second set).\r\nprint(\"firstset-secondset : \", rslt1)\r\n# Print the result\u00a0(second set - first set ).\r\nprint(\"secondset-firstset : \", rslt2)\r\n<\/pre>\n
firstset-secondset : {10, 13}\r\nsecondset-firstset : {20, 14}<\/pre>\n
2)Set Difference Using ‘-‘ Operator<\/h5>\n
\n
# Give the first set as static input and store it in a variable.\r\nfst_set = {'u', 'v', 'w', 'x', 'y', 'u'}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {'w', 'z', 'x', 'a'}\r\n# Get the difference between the( first set - second set ) using the '-' operator\r\n# and print it\r\nprint(\"firstset-secondset : \", fst_set-scnd_set)\r\n# Get the difference between the (second set - first set ) using the '-' operator\r\n# print it.\r\nprint(\"secondset-firstset : \", scnd_set-fst_set)\r\n<\/pre>\n
firstset-secondset : {'y', 'v', 'u'}\r\nsecondset-firstset : {'z', 'a'}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)Set Difference Using difference() method<\/h5>\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# Get the difference between the( first set - second set ) using the difference()\r\n# method.\r\n# Store it in another variable.\r\nrslt1 = fst_set.difference(scnd_set)\r\n# Get the difference between the (second set - first set ) using the difference()\r\n# method.\r\n# Store it in another variable.\r\nrslt2 = scnd_set.difference(fst_set)\r\n# Print the result (first set - second set).\r\nprint(\"firstset-secondset : \", rslt1)\r\n# Print the result (second set - first set ).\r\nprint(\"secondset-firstset : \", rslt2)\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 20 30 40 10 10\r\nEnter some random Set Elements separated by spaces = 60 50 20 30\r\nfirstset-secondset : {40, 10}\r\nsecondset-firstset : {50, 60}<\/pre>\n
2)Set Difference Using ‘-‘ Operator<\/h5>\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# Get the difference between the( first set - second set ) using the '-' operator\r\n# and print it\r\nprint(\"firstset-secondset : \", fst_set-scnd_set)\r\n# Get the difference between the (second set - first set ) using the '-' operator\r\n# print it.\r\nprint(\"secondset-firstset : \", scnd_set-fst_set)\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 10 10 13 15\r\nEnter some random Set Elements separated by spaces = 12 15\r\nfirstset-secondset : {10, 13}\r\nsecondset-firstset : {12}<\/pre>\n