BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Set symmetric_difference() method with examples

Last Updated: April 1, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

Set symmetric_difference() method returns a symmetric difference of two given sets. A symmetric difference of two sets X and Y contains the elements that are in either set X or Set Y but not in both. For example – symmetric difference of set {1, 2, 3} and {2, 3, 4} would be {1, 4} because elements 2 and 3 are present in both the sets.

Set symmetric_difference() syntax

set.symmetric_difference(another_set)

Parameter: It takes a set as a parameter
Return Value: It returns a new set which is a symmetric difference of the two given sets.

Python Set symmetric_difference() Example

In the following example we have three sets X, Y and Z. Sets Y and Z are same. When we find the symmetric difference between same sets it returns nothing, as shown in the output of the following example. We can also find the symmetric difference using ^ operator, which is discussed in the next section of this same article.

# Set X
X = {1, 2, 3}

# Set Y
Y = {2, 3, 4}

# Set Z
Z = {2, 3, 4}

print("Symmetric difference between X & Y", X.symmetric_difference(Y))
print("Symmetric difference between Y & Z", Y.symmetric_difference(Z))
print("Symmetric difference between X & X", X.symmetric_difference(X))

Output:
Python symmetric_difference() method example

Finding the symmetric difference between two sets using ^ operator

We can use the ^ operator instead of symmetric_difference() method to find the symmetric difference between two sets as shown in the following example.

# Set X
X = {1, 2, 3}

# Set Y
Y = {2, 3, 4}

# Set Z
Z = {2, 3, 4}

print(X^Y)
print(X^Z)
print(Y^Z)

# symmetric difference with self
print(X^X)
print(Y^Y)

Output:

{1, 4}
{1, 4}
set()
set()
set()

Top Related Articles:

  1. Python Data Types
  2. Python Set update() method with examples
  3. Python Set discard() method with examples
  4. Python Set intersection_update() method with examples
  5. Python Set pop() method with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap