  Set of pythons | symmetric_difference ()

Python Methods and Functions Symmetric difference marked in green

If there is set_A and set_B, then the symmetric difference between them will be equal to the union of set_A and set_B without intersection between them.

// Takes a single parameter that has to be // a set and returns a  new set  which is the // symmetric difference between the two sets. set_A.symmetric_difference (set_B)

Examples:

Input: set_A = {1, 2, 3, 4, 5} set_B = {6, 7, 3, 9, 4} Output: {1, 2, 5, 6, 7, 9} Explanation: The common elements {3, 4} are discarded from the output. Input: set_A = {"ram", "rahim", "ajay", "rishav", "aakash"} set_B = {"aakash", "ajay", "shyam", "ram", "ravi"} Output: {"rahim", "rishav", "shyam", "ravi"} Explanation: The common elements {"ram", "ajay", "aakash"} are discarded from the final set

This In the program we will try to find a symmetrical difference between the two sets:

 # Python code for symbric_difference search # Using the symbric_difference () method   set_A = { 1 , 2 , 3 , 4 , 5  } set_B = { 6 , 7 , 3 , 9 , 4 } print (set_A.symmetric_difference (set_B))

Output:

{1, 2, 5, 6, 7, 9}

There is also another method to obtain the symmetric difference between two sets using the " ^ " operator.
Example:

 # Python code to find symmetric difference # using the operator ^.   Driver code set_A = { "ram " , " rahim " , "ajay" , "rishav" , "aakash" } set_B = { "aakash" , "ajay" , "shyam" , "ram" , "ravi" } print (set_A ^ set_B)

Output:

{'shyam',' ravi', 'rahim',' rishav'}

 # More one example Python code for searching # using simricric_difference # mmetric_difference () method   A = { 'p' , ' a' , 'w' , ' a' , 'n' } B = { 'r' , ' a' , 'o' , ' n' , 'e' } C = {}   print (A.symmetric_difference ( B)) print (B.symmetric_difference (A))   print (A.symmetric_differenc e (C)) print (B.symmetric_difference (C))   # this example is provided by sunny6041

Exit:

set (['e',' o' , 'p',' r', 'w']) set ([' e', 'o',' p', 'r',' w']) set (['a',' p', ' w', 'n']) set ([' a', 'r',' e', 'o',' n'])