• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Symmetric Difference of Two Sets in Python

Symmetric Difference of Two Sets in Python

March 9, 2022 Leave a Comment

In Python, we can find the symmetric difference of two sets easily with the set symmetric_difference() function.

a = {0, 1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}

print(a.symmetric_difference(b))

#Output:
{0, 1, 2, 6, 7, 8}

You can also get the symmetric difference of two sets with the ^ operator.

a = {0, 1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}

print(a^ b)

#Output:
{0, 1, 2, 6, 7, 8}

In Python, sets are unordered collections of items. When working with sets, it can be useful to know all of the elements which are only in one set, but not the other set.

The symmetric difference of two sets is the set of elements that are in either of the sets, but not in the intersection of the sets.

We can easily find the symmetric difference of two sets in Python with the set symmetric_difference() function.

Below is a simple Python example of how to find the symmetric difference of two sets.

a = {0, 1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}

print(a.symmetric_difference(b))

#Output:
{0, 1, 2, 6, 7, 8}

Symmetric Difference of Two Lists in Python

If you are working with lists and need to find the symmetric difference of two lists, you can easily do so by converting the lists to sets.

To convert a list to a set, use set(). Then we can call the symmetric_difference() function on the new set and pass the other converted list.

Below is an example of how to get the symmetric difference of two lists in Python.

a = [0, 1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7, 8]

print(set(a).symmetric_difference(set(b)))

#Output:
{0, 1, 2, 6, 7, 8}

Symmetric Difference of Two Sets with ^ Operator in Python

You can also get the symmetric difference of two sets with the ^ operator. The ^ operator gets all elements which are in the first set but not in the second set, and in the second set but not in the first set.

Below is how to get the symmetric difference of two sets using the ^ operator in Python.

a = {0, 1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}

print(a^ b)

#Output:
{0, 1, 2, 6, 7, 8}

Hopefully this article has been useful for you to learn how to find the symmetric difference of sets in Python.

Other Articles You'll Also Like:

  • 1.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 2.  Find Index of Maximum in List Using Python
  • 3.  How to Hide Turtle in Python with hideturtle() Function
  • 4.  Convert False to 0 in Python
  • 5.  Backwards for Loop in Python
  • 6.  Convert Integer to Bytes in Python
  • 7.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 8.  Python Split List into N Sublists
  • 9.  Get Difference Between datetime Variables in Python
  • 10.  pandas Correlation – Find Correlation of Series or DataFrame Columns

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy