×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Set symmetric_difference() Method (with Examples)

Last Updated : December 11, 2025

Python Set symmetric_difference() Method

The symmetric_difference() is an inbuilt method of the set class that is used to get the list of all elements which are not common in both sets, the method is called with this set (set1), and another set (set2) is supplied as an argument. And, returns a set of those uncommon elements.

Syntax

The following is the syntax of symmetric_difference() method:

set1.symmetric_difference(set2)

Parameter(s):

The following are the parameter(s):

  • set2 – It represents another set to be compared with this set (set1).

Return Value

The return type of this method is <class 'set'>, it returns the set of those elements which are not common in both sets.

Example 1: Use of Set symmetric_difference() Method

# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}

# symmetric_difference() method call
x = cars_1.symmetric_difference(cars_2)

# printing the sets
print("cars_1:", cars_1)
print("cars_2:", cars_2)
print("x:", x)

Output

cars_1: {'Lexus', 'Audi', 'Porsche'}
cars_2: {'Porsche', 'Mazda', 'Lincoln'}
x: {'Lexus', 'Lincoln', 'Audi', 'Mazda'}

Example 2: Use of Set symmetric_difference() Method

# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "ABC"}

# printing the results
print("x:", x)
print("y:", y)
print("z:", z)

# printing the result of symmetric_difference()
print("x.symmetric_difference(y): ", x.symmetric_difference(y))
print("x.symmetric_difference(z): ", x.symmetric_difference(z))
print("y.symmetric_difference(x): ", y.symmetric_difference(x))
print("y.symmetric_difference(z): ", y.symmetric_difference(z))
print("z.symmetric_difference(y): ", z.symmetric_difference(y))
print("z.symmetric_difference(x): ", z.symmetric_difference(x))

Output

x: {'XYZ', 'ABC', 'PQR'}
y: {'XYZ', 'ABC', 'PQR'}
z: {'MNO', 'ABC', 'DEF'}
x.symmetric_difference(y):  set()
x.symmetric_difference(z):  {'PQR', 'MNO', 'XYZ', 'DEF'}
y.symmetric_difference(x):  set()
y.symmetric_difference(z):  {'PQR', 'MNO', 'XYZ', 'DEF'}
z.symmetric_difference(y):  {'PQR', 'MNO', 'XYZ', 'DEF'}
z.symmetric_difference(x):  {'PQR', 'MNO', 'XYZ', 'DEF'}
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.