{"id":99,"date":"2024-01-25T10:30:50","date_gmt":"2024-01-25T10:30:50","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=99"},"modified":"2024-01-25T10:30:50","modified_gmt":"2024-01-25T10:30:50","slug":"python-set-difference","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-set-difference\/","title":{"rendered":"Python Set Difference"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python Set difference and how to use it to find the difference between two or more sets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python Set difference<\/h2>\n\n\n\n<p>The difference between the two\u00a0sets\u00a0results in a new set that has elements from the first set which aren\u2019t present in the second set.<\/p>\n\n\n\n<p>Suppose you have the following\u00a0<code>s1<\/code>\u00a0and\u00a0<code>s2<\/code>\u00a0sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>The difference between\u00a0<code>s1<\/code>\u00a0and\u00a0<code>s2<\/code>\u00a0sets results in the following set with one element:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'Python'} <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>\u2026because there is only&nbsp;<code>'Python'<\/code>&nbsp;element from the first set that doesn\u2019t exist in the second set.<\/p>\n\n\n\n<p>The set difference isn\u2019t commutative. The difference between the\u00a0<code>s2<\/code>\u00a0and\u00a0<code>s1<\/code>\u00a0sets returns the following set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'C#'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>The following Venn diagram illustrates the difference between the&nbsp;<code>s1<\/code>&nbsp;and&nbsp;<code>s2<\/code>&nbsp;sets:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Set-Difference.png\" alt=\"Python set difference\" class=\"wp-image-614\"\/><\/figure>\n\n\n\n<p>And the following Venn diagram illustrates the difference between&nbsp;<code>s2<\/code>&nbsp;and&nbsp;<code>s1<\/code>&nbsp;sets:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Set-Difference-Example.png\" alt=\"Python set difference example\" class=\"wp-image-615\"\/><\/figure>\n\n\n\n<p>In Python, you can use the set&nbsp;<code>difference()<\/code>&nbsp;method or set difference operator (<code>-<\/code>) to find the difference between sets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using Python Set difference() method to find the difference between sets<\/h3>\n\n\n\n<p>The\u00a0<code>Set<\/code>\u00a0type has a\u00a0<code>difference()<\/code>\u00a0method that returns the difference between two or more sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>set1.difference(s2, s3, ...)<\/code><small>Code language: CSS (css)<\/small><\/code><\/pre>\n\n\n\n<p>For example, you can use the set\u00a0<code>difference()<\/code>\u00a0method to find the difference between\u00a0<code>s1<\/code>\u00a0and\u00a0<code>s2<\/code>\u00a0sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s1.difference(s2) print(s)<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'Python'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>And this example shows how to use the set\u00a0<code>difference()<\/code>\u00a0method to find the difference between\u00a0<code>s2<\/code>\u00a0and\u00a0<code>s1<\/code>\u00a0sets.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s2.difference(s1) print(s)<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'C#'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>Note that the&nbsp;<code>difference()<\/code>&nbsp;method returns a new set. It doesn\u2019t change the original sets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using Python set difference operator (-) to find the difference between sets<\/h3>\n\n\n\n<p>Besides the&nbsp;<code>difference()<\/code>&nbsp;method, Python provides you with the set difference operator (<code>-<\/code>) that allows you to find the difference between sets.<code>s = s1 - s2<\/code><\/p>\n\n\n\n<p>The following example uses the difference operator (<code>-<\/code>) to find the difference between the\u00a0<code>s1<\/code>\u00a0and\u00a0<code>s2<\/code>\u00a0sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s1 - s2 print(s)<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>And this example use the set difference operator to return the difference between\u00a0<code>s2<\/code>\u00a0and\u00a0<code>s1<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s2 - s1 print(s) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'C#'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The set difference() method vs set difference operator (-)<\/h3>\n\n\n\n<p>The set\u00a0<code>difference()<\/code>\u00a0method can accept one or more\u00a0iterables\u00a0(e.g.,\u00a0strings,\u00a0lists,\u00a0dictionaries) while the set difference operator (<code>-<\/code>) only allows sets.<\/p>\n\n\n\n<p>When you pass iterables to the set&nbsp;<code>difference()<\/code>&nbsp;method, it\u2019ll convert the iterables to sets before performing the difference operation.<\/p>\n\n\n\n<p>The following shows how to use the set\u00a0<code>difference()<\/code>\u00a0method with a list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>scores = {7, 8, 9} numbers = &#91;9, 10] new_scores = scores.difference(numbers) print(new_scores) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>However, if you use the set difference operator (<code>-<\/code>) with iterables, you\u2019ll get an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>scores = {7, 8, 9} numbers = &#91;9, 10] new_scores = scores - numbers print(new_scores) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>TypeError: unsupported operand type(s) for -: 'set' and 'list'<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python Set difference and how to use it to find the difference between two or more sets. Introduction to the Python Set difference The difference between the two\u00a0sets\u00a0results in a new set that has elements from the first set which aren\u2019t present in the second set. Suppose [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-7-sets"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/99\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}