{"id":25365,"date":"2021-11-10T09:37:36","date_gmt":"2021-11-10T04:07:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=25365"},"modified":"2021-11-10T09:37:36","modified_gmt":"2021-11-10T04:07:36","slug":"python-set-symmetric_difference-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-symmetric_difference-method-with-examples\/","title":{"rendered":"Python Set symmetric_difference() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set symmetric_difference() Method in Python:<\/strong><\/p>\n The symmetric_difference() method returns a set that includes all items from both sets but excludes items that are present in both sets.<\/p>\n That means the returned set contains a mix of items that aren’t in either set.<\/p>\n For Example:<\/strong><\/p>\n If P and Q are two distinct sets. The\u00a0 Symmetric difference of given two sets is :<\/p>\n The set of elements that are in either P or Q but not in their intersection is the symmetric difference.<\/p>\n Let P={4, 5, 6, 7}<\/p>\n Q={5, 6, 8, 9}<\/p>\n Here {5, 6} are the common elements in both sets. so, we exclude them.<\/p>\n The symmetric difference of P and Q = {4, 7, 8, 9}<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set:<\/strong> This is Required. The set to look for matches in.<\/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 Prerequisite: Python set() Function with Examples Set symmetric_difference() Method in Python: The symmetric_difference() method returns a set that includes all items from both sets but excludes items that are present in both sets. That means the returned set contains a mix of items that aren’t in either set. For Example: If P and Q are …<\/p>\nset.symmetric_difference(set)<\/pre>\n
Given first set = {10, 20, 50, 60, 30}\r\nGiven second set = {20, 60, 80}<\/pre>\n
The symmetric difference of first and secondset = {10, 80, 50, 30}<\/pre>\n
Given first set = {4, 5, 6, 7}\r\nGiven second set = {5, 6, 8, 9}<\/pre>\n
The symmetric difference of first and secondset = {4, 7, 8, 9}<\/pre>\n
Set symmetric_difference() 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 = {10, 20, 50, 60, 30}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {20, 60, 80}\r\n# Give the third set as static input and store it in another variable.\r\nthrd_set = {}\r\n# Get the symmetric difference of the first set and second set using\r\n# the symmetric_difference() method and print it.\r\nprint(\"The symmetric difference of first and secondset = \",\r\n fst_set.symmetric_difference(scnd_set))\r\n# Get the symmetric difference of the second set and first set using\r\n# the symmetric_difference() method and print it.\r\nprint(\"The symmetric difference of second and firstset = \",\r\n scnd_set.symmetric_difference(fst_set))\r\n# Get the symmetric difference of the first set and third set using\r\n# the symmetric_difference() method and print it.\r\nprint(\"The symmetric difference of first and thirdset = \",\r\n fst_set.symmetric_difference(thrd_set))\r\n<\/pre>\n
The symmetric difference of first and secondset = {10, 80, 50, 30}\r\nThe symmetric difference of second and firstset = {80, 50, 10, 30}\r\nThe symmetric difference of first and thirdset = {10, 50, 20, 60, 30}<\/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# Give the third set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nthrd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n \r\n# Get the symmetric difference of the first set and second set using\r\n# the symmetric_difference() method and print it.\r\nprint(\"The symmetric difference of first and secondset = \",\r\n fst_set.symmetric_difference(scnd_set))\r\n# Get the symmetric difference of the second set and first set using\r\n# the symmetric_difference() method and print it.\r\nprint(\"The symmetric difference of second and firstset = \",\r\n scnd_set.symmetric_difference(fst_set))\r\n# Get the symmetric difference of the first set and third set using\r\n# the symmetric_difference() method and print it.\r\nprint(\"The symmetric difference of first and thirdset = \",\r\n fst_set.symmetric_difference(thrd_set))\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 3 4 5 6\r\nEnter some random Set Elements separated by spaces = 3 7 8 9\r\nEnter some random Set Elements separated by spaces = 9 5 7 1\r\nThe symmetric difference of first and secondset = {4, 5, 6, 7, 8, 9}\r\nThe symmetric difference of second and firstset = {4, 5, 6, 7, 8, 9}\r\nThe symmetric difference of first and thirdset = {1, 3, 4, 6, 7, 9}<\/pre>\n","protected":false},"excerpt":{"rendered":"