{"id":621,"date":"2020-10-17T10:51:53","date_gmt":"2020-10-17T10:51:53","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=621"},"modified":"2025-03-27T03:05:28","modified_gmt":"2025-03-27T03:05:28","slug":"python-symmetric-difference","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-symmetric-difference\/","title":{"rendered":"Python Symmetric Difference"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to find the symmetric difference between two or more sets in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-symmetric-difference-of-sets'>Introduction to the symmetric difference of sets <a href=\"#introduction-to-the-symmetric-difference-of-sets\" class=\"anchor\" id=\"introduction-to-the-symmetric-difference-of-sets\" title=\"Anchor for Introduction to the symmetric difference of sets\">#<\/a><\/h2>\n\n\n\n<p>The symmetric difference between two <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-set\/\">sets<\/a> is a set of elements that are in either set, but not in their <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-set-intersection\/\">intersection<\/a>.<\/p>\n\n\n\n<p>Suppose that you have the following <code>s1<\/code> and <code>s2<\/code> sets:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">s1 = {<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C++'<\/span>}\ns2 = {<span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C++'<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The symmetric difference of the <code>s1<\/code> and <code>s2<\/code> sets returns in the following set:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">{<span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Python'<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see clearly from the output, the elements in the return set are either in <code>s1<\/code> or <code>s2<\/code> set, but not in their intersection.<\/p>\n\n\n\n<p>The following Venn diagram illustrates the symmetric difference of the <code>s1<\/code> and <code>s2<\/code> sets:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"163\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Symmetric-Difference-Python.png\" alt=\"Python symmetric difference of two sets\" class=\"wp-image-622\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Symmetric-Difference-Python.png 683w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Symmetric-Difference-Python-300x72.png 300w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/figure>\n\n\n\n<p>In Python, you can find the symmetric difference of two or more sets by using the set <code>symmetric_difference()<\/code> method or the symmetric difference operator (<code>^<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-the-symmetric_difference-method-to-find-the-symmetric-difference-of-sets'>Using the symmetric_difference() method to find the symmetric difference of sets <a href=\"#using-the-symmetric_difference-method-to-find-the-symmetric-difference-of-sets\" class=\"anchor\" id=\"using-the-symmetric_difference-method-to-find-the-symmetric-difference-of-sets\" title=\"Anchor for Using the symmetric_difference() method to find the symmetric difference of sets\">#<\/a><\/h3>\n\n\n\n<p>The <code>Set<\/code> type has the <code>symmetric_difference()<\/code> method that returns the symmetric difference of two or more sets:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">new_set = set1.symmetric_difference(set2, set3,...)<\/code><\/span><\/pre>\n\n\n<p>For example, the following finds the symmetric difference of the <code>s1<\/code> and <code>s2<\/code> sets:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">s1 = {<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C++'<\/span>}\ns2 = {<span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C++'<\/span>}\n\ns = s1.symmetric_difference(s2)\n\n<span class=\"hljs-keyword\">print<\/span>(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=czEgPSB7J1B5dGhvbicsICdKYXZhJywgJ0MrKyd9CnMyID0geydDIycsICdKYXZhJywgJ0MrKyd9CgpzID0gczEuc3ltbWV0cmljX2RpZmZlcmVuY2UoczIpCgpwcmludChzKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">{<span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Python'<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the <code>symmetric_difference()<\/code> method returns a new set and doesn&#8217;t modify the original sets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-the-symmetric-difference-operator-to-find-the-symmetric-difference-of-sets'>Using the symmetric difference operator(^) to find the symmetric difference of sets <a href=\"#using-the-symmetric-difference-operator-to-find-the-symmetric-difference-of-sets\" class=\"anchor\" id=\"using-the-symmetric-difference-operator-to-find-the-symmetric-difference-of-sets\" title=\"Anchor for Using the symmetric difference operator(^) to find the symmetric difference of sets\">#<\/a><\/h3>\n\n\n\n<p>Besides using the set <code>symmetric_difference()<\/code> method, you can use the symmetric difference operator (<code>^<\/code>) to find the symmetric difference between two or more sets:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">new_set = set1 ^ set2 ^...<\/code><\/span><\/pre>\n\n\n<p>The following example shows how to apply the symmetric difference operator (<code>^<\/code>) to the <code>s1<\/code> and <code>s2<\/code> sets:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">s1 = {<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C++'<\/span>}\ns2 = {<span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C++'<\/span>}\n\ns = s1 ^ s2\n\n<span class=\"hljs-keyword\">print<\/span>(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=czEgPSB7J1B5dGhvbicsICdKYXZhJywgJ0MrKyd9CnMyID0geydDIycsICdKYXZhJywgJ0MrKyd9CgpzID0gczEgXiBzMgoKcHJpbnQocyk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">{<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'C#'<\/span>}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='the-symmetric_difference-method-vs-symmetric-difference-operator'>The symmetric_difference() method vs symmetric difference operator (^) <a href=\"#the-symmetric_difference-method-vs-symmetric-difference-operator\" class=\"anchor\" id=\"the-symmetric_difference-method-vs-symmetric-difference-operator\" title=\"Anchor for The symmetric_difference() method vs symmetric difference operator (^)\">#<\/a><\/h3>\n\n\n\n<p>The <code>symmetric_difference()<\/code> method accepts one or more <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-iterables\/\">iterables<\/a> that can be <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string\/\">strings<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">lists<\/a>, or <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-dictionary\/\">dictionaries<\/a>.<\/p>\n\n\n\n<p>If the iterables aren&#8217;t sets, the method will convert them to sets before returning the symmetric difference of them.<\/p>\n\n\n\n<p>The following example shows how to use the <code>symmetric_difference()<\/code> method to find the symmetric difference between a set and a list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">scores = {<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>}\nratings = &#91;<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\nnew_set = scores.symmetric_difference(ratings)\n\n<span class=\"hljs-keyword\">print<\/span>(new_set)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=c2NvcmVzID0gezcsIDgsIDl9CnJhdGluZ3MgPSBbOCwgOSwgMTBdCm5ld19zZXQgPSBzY29yZXMuc3ltbWV0cmljX2RpZmZlcmVuY2UocmF0aW5ncykKCnByaW50KG5ld19zZXQp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">{10, 7}<\/code><\/span><\/pre>\n\n\n<p>However, the symmetric difference operator (<code>^<\/code>) only applies to sets. If you use it with the iterables which aren&#8217;t sets, you&#8217;ll get an error. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">scores = {<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>}\nratings = &#91;<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\nnew_set = scores ^ ratings\n\n<span class=\"hljs-keyword\">print<\/span>(new_set)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=c2NvcmVzID0gezcsIDgsIDl9CnJhdGluZ3MgPSBbOCwgOSwgMTBdCm5ld19zZXQgPSBzY29yZXMgXiByYXRpbmdzCgpwcmludChuZXdfc2V0KQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">TypeError<\/span>: unsupported operand type(s) <span class=\"hljs-keyword\">for<\/span> ^: <span class=\"hljs-string\">'set'<\/span> and <span class=\"hljs-string\">'list'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The symmetric difference between two or more sets is a set of elements that are in all sets, but not in their intersections.<\/li>\n\n\n\n<li>Use the set <code>symmetric_difference()<\/code> method or the symmetric difference operator (<code>^<\/code>) to find the symmetric difference of two or more sets.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=set-difference\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"621\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-symmetric-difference\/\"\n\t\t\t\tdata-post-title=\"Python Symmetric Difference\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"621\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-symmetric-difference\/\"\n\t\t\t\tdata-post-title=\"Python Symmetric Difference\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to find the symmetric difference of two or more sets in Python.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":45,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-621","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":4,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/621\/revisions"}],"predecessor-version":[{"id":7072,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/621\/revisions\/7072"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}