{"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":"
Prerequisite:<\/h5>\n

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

set.symmetric_difference(set)<\/pre>\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

Given first set = {10, 20, 50, 60, 30}\r\nGiven second set = {20, 60, 80}<\/pre>\n

Output:<\/strong><\/p>\n

The symmetric difference of first and secondset =  {10, 80, 50, 30}<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given first set = {4, 5, 6, 7}\r\nGiven second set = {5, 6, 8, 9}<\/pre>\n

Output:<\/strong><\/p>\n

The symmetric difference of first and secondset =  {4, 7, 8, 9}<\/pre>\n

Set symmetric_difference() Method with Examples in Python<\/h2>\n