{"id":25376,"date":"2022-01-03T09:10:18","date_gmt":"2022-01-03T03:40:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=25376"},"modified":"2022-01-03T09:10:18","modified_gmt":"2022-01-03T03:40:18","slug":"python-set-symmetric_difference_update-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-symmetric_difference_update-method-with-examples\/","title":{"rendered":"Python Set symmetric_difference_update() Method with Examples"},"content":{"rendered":"
Set symmetric_difference_update() Method:<\/strong><\/p>\n The symmetric_difference_update() method updates the original set by removing items from both sets and inserting new items.<\/p>\n The set of elements that are in either P or Q but not in their intersection is the symmetric difference of two sets P and Q.<\/p>\n For Example:<\/strong><\/p>\n If P and Q are two distinct sets. The symmetric_difference_update of the given two sets is :<\/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.<\/p>\n so, Set P is updated with all the other items from both sets except the common elements.<\/p>\n Set Q, on the other hand, remains unchanged.<\/p>\n Output : P={4, 7, 8, 9} and Q={5, 6, 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 Return Value:<\/strong><\/p>\n None is returned by symmetric difference update() (returns nothing). Rather, it calls the set and updates it.<\/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 Set symmetric_difference_update() Method: The symmetric_difference_update() method updates the original set by removing items from both sets and inserting new items. The set of elements that are in either P or Q but not in their intersection is the symmetric difference of two sets P and Q. For Example: If P and Q are two distinct …<\/p>\nset.symmetric_difference_update(set)<\/pre>\n
Given first set = {10, 20, 50, 60, 30}\r\nGiven second set = {20, 30, 80, 90}<\/pre>\n
The given first set = {10, 80, 50, 90, 60}\r\nThe given second set = {80, 90, 20, 30}\r\nThe result after applying symmetric_difference_update() method = None<\/pre>\n
Given first set = {'a', 'b', 'c'}\r\nGiven second set = {'a', 'b', 'c', 'd', 'e'}<\/pre>\n
The given first set = {'d', 'e'}\r\nThe given second set = {'a', 'e', 'b', 'd', 'c'}\r\nThe result after applying symmetric_difference_update() method = None<\/pre>\n
Set symmetric_difference_update() 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, 30, 80, 90}\r\n# Apply symmetric_difference_update() method to the given two sets and\r\n# Store it in another variable.\r\nrslt = fst_set.symmetric_difference_update(scnd_set)\r\n# Print the given first set\r\nprint(\"The given first set = \", fst_set)\r\n# Print the given second set\r\nprint(\"The given second set = \", scnd_set)\r\n# Print the result after applying symmetric_difference_update() method for the\r\n# given two sets\r\nprint(\"The result after applying symmetric_difference_update() method = \", rslt)\r\n<\/pre>\n
The given first set = {10, 80, 50, 90, 60}\r\nThe given second set = {80, 90, 20, 30}\r\nThe result after applying symmetric_difference_update() method = None<\/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 symmetric_difference_update() method to the given two sets and\r\n# Store it in another variable.\r\nrslt = fst_set.symmetric_difference_update(scnd_set)\r\n# Print the given first set\r\nprint(\"The given first set = \", fst_set)\r\n# Print the given second set\r\nprint(\"The given second set = \", scnd_set)\r\n# Print the result after applying symmetric_difference_update() method for the\r\n# given two sets\r\nprint(\"The result after applying symmetric_difference_update() method = \", rslt)\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 2 5 6 7 1\r\nEnter some random Set Elements separated by spaces = 6 9 1 4 0\r\nThe given first set = {0, 2, 4, 5, 7, 9}\r\nThe given second set = {0, 1, 4, 6, 9}\r\nThe result after applying symmetric_difference_update() method = None<\/pre>\n","protected":false},"excerpt":{"rendered":"