<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Sets - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Sets - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 26 Aug 2026 20:17:55 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/sets/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Python Sets – Operations and Examples ]]>
                </title>
                <description>
                    <![CDATA[ If you're a beginner to Python, chances are you've come across lists. But have you heard about sets in Python? In this tutorial, we'll explore what sets are, how to create them, and the different operations you can use on them. What are sets in Pytho... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-set-operations-explained-with-examples/</link>
                <guid isPermaLink="false">66d45f3d47a8245f78752a5e</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Sets ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Jason ]]>
                </dc:creator>
                <pubDate>Thu, 28 Oct 2021 18:17:35 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/10/python-sets-article-image.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're a beginner to Python, chances are you've <a target="_blank" href="https://www.freecodecamp.org/news/lists-in-python-comprehensive-guide/">come across lists</a>. But have you heard about sets in Python?</p>
<p>In this tutorial, we'll explore what sets are, how to create them, and the different operations you can use on them.</p>
<h1 id="heading-what-are-sets-in-python">What are sets in Python?</h1>
<p>In Python, sets are exactly like lists except for the fact that their elements are <em>immutable</em> (that means you cannot change/mutate an element of a set once declared). However, you can add/remove elements from the set.</p>
<p>If that was confusing, let me try and summarize:</p>
<blockquote>
<p>A set is a mutable, unordered group of elements, where the elements themselves are immutable.</p>
</blockquote>
<p>Another characteristic of a set is that it may include elements of different types. This means you can have a group of numbers, strings, and even tuples, all in the same set!</p>
<h1 id="heading-how-to-create-a-set">How to Create a Set</h1>
<p>The most common way of creating a set in Python is by using the built-in <code>set()</code> function.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>first_set = set((<span class="hljs-string">"Connor"</span>, <span class="hljs-number">32</span>, (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)))
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set
{<span class="hljs-number">32</span>, <span class="hljs-string">'Connor'</span>, (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)}
<span class="hljs-meta">&gt;&gt;&gt; </span>
<span class="hljs-meta">&gt;&gt;&gt; </span>second_set = set(<span class="hljs-string">"Connor"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>second_set
{<span class="hljs-string">'n'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'o'</span>}
</code></pre>
<p>You can also create sets using the curly brace <code>{}</code> syntax:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>third_set = {<span class="hljs-string">"Apples"</span>, (<span class="hljs-string">"Bananas"</span>, <span class="hljs-string">"Oranges"</span>)}
<span class="hljs-meta">&gt;&gt;&gt; </span>type(third_set)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">set</span>'&gt;</span>
</code></pre>
<p>The <code>set()</code> function takes in an <em>iterable</em> and yields a list of objects which will be inserted into the set. The <code>{}</code> syntax places the objects themselves into the set.</p>
<p>As you've probably realized, whether you use the <code>set()</code> function or the <code>{}</code> to create a set, each element needs to be an immutable object. So if you add a list (which is a mutable object) to a set, you'll run into an error:</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>incorrect_set = {<span class="hljs-string">"Apples"</span>, [<span class="hljs-string">"Bananas"</span>, <span class="hljs-string">"Oranges"</span>]}
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: unhashable type: <span class="hljs-string">'list'</span>
</code></pre>
<h1 id="heading-how-to-add-or-remove-elements-in-a-set">How to Add or Remove Elements in a Set</h1>
<p>We already know that sets are mutable. This means you can add/remove elements in a set.</p>
<p>Here's an example of adding elements to a set using the <code>update()</code> function.</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>add_set = set((<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>add_set
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>add_set.update((<span class="hljs-number">1</span>,))
<span class="hljs-meta">&gt;&gt;&gt; </span>add_set
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>add_set.update((<span class="hljs-string">"cello"</span>, <span class="hljs-string">"violin"</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>add_set
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-string">'violin'</span>, <span class="hljs-string">'cello'</span>}
</code></pre>
<p>But notice how nothing changes when we try to add "cello" to the set again:</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>add_set.update((<span class="hljs-string">"cello"</span>,))
<span class="hljs-meta">&gt;&gt;&gt; </span>add_Set
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-string">'violin'</span>, <span class="hljs-string">'cello'</span>}
</code></pre>
<p>This is because sets in Python <em>cannot</em> contain duplicates. So, when we tried to add <code>"cello"</code> again to the set, Python recognized we were trying to add a duplicate element and didn't update the set. This is one caveat that differentiates sets from lists.</p>
<p>Here's how you would remove elements from a set:</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>sub_set = add_set
<span class="hljs-meta">&gt;&gt;&gt; </span>sub_set.remove(<span class="hljs-string">"violin"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>sub_set
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-string">'cello'</span>}
</code></pre>
<p>The <code>remove(x)</code> function removes the element <code>x</code> from a set. It returns a <code>KeyError</code> if <code>x</code> is not part of the set:</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>sub_set.remove(<span class="hljs-string">"guitar"</span>)
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
KeyError: <span class="hljs-string">'guitar'</span>
</code></pre>
<p>There are a couple of other ways to remove an element(s) from a set:</p>
<ul>
<li><p>the <code>discard(x)</code> method removes <code>x</code> from the set, but <em>doesn't</em> raise any error if <code>x</code> is not present in the set.</p>
</li>
<li><p>the <code>pop()</code> method removes and returns a random element from the set.</p>
</li>
<li><p>the <code>clear()</code> method removes all elements from a set</p>
</li>
</ul>
<p>Here are some examples to illustrate:</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>m_set = set((<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>
<span class="hljs-meta">&gt;&gt;&gt; </span>m_set.discard(<span class="hljs-number">5</span>) <span class="hljs-comment"># no error raised even though '5' is not present in the set</span>
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>m_set.pop()
<span class="hljs-number">4</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>m_set
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>m_set.clear()
<span class="hljs-meta">&gt;&gt;&gt; </span>m_set
set()
</code></pre>
<h1 id="heading-python-set-operations">Python set() Operations</h1>
<p>If you remember your basic high school math, you'll probably recall mathematical set operations like <em>union</em>, <em>intersection</em>, <em>difference</em> and <em>symmetric difference</em>. Well, you can achieve the same thing with Python sets.</p>
<h2 id="heading-1-set-union">1. Set Union</h2>
<p>The union of two sets is the set of <em>all the elements</em> of both the sets without duplicates. You can use the <code>union()</code> method or the <code>|</code> syntax to find the union of a Python set.</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>first_set = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>second_set = {<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set.union(second_set)
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set | second_set     <span class="hljs-comment"># using the `|` operator</span>
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}
</code></pre>
<h2 id="heading-2-set-intersection">2. Set Intersection</h2>
<p>The intersection of two sets is the set of <em>all the common elements</em> of both the sets. You can use the <code>intersection()</code> method of the <code>&amp;</code> operator to find the intersection of a Python set.</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>first_set = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>second_set = {<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set.intersection(second_set)
{<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set &amp; second_set     <span class="hljs-comment"># using the `&amp;` operator</span>
{<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}
</code></pre>
<h2 id="heading-3-set-difference">3. Set Difference</h2>
<p>The difference between two sets is the set of all the elements in first set that <em>are not</em> present in the second set. You would use the <code>difference()</code> method or the <code>-</code> operator to achieve this in Python.</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>first_set = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>second_set = {<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set.difference(second_set)
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set - second_set     <span class="hljs-comment"># using the `-` operator</span>
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>second_set - first_set
{<span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">7</span>}
</code></pre>
<h2 id="heading-4-set-symmetric-difference">4. Set Symmetric Difference</h2>
<p>The symmetric difference between two sets is the set of all the elements that are <em>either in</em> the first set <em>or</em> the second set <em>but not in both</em>.</p>
<p>You have the choice of using either the <code>symmetric_difference()</code> method or the <code>^</code> operator to do this in Python.</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>first_set = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>second_set = {<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set.symmetric_difference(second_set)
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>first_set ^ second_set     <span class="hljs-comment"># using the `^` operator</span>
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
</code></pre>
<h1 id="heading-how-to-modify-a-set-by-operations">How to Modify a Set by Operations</h1>
<p>Each of the <code>set()</code> operations that we discussed above can be used to <em>modify</em> an existing Python set. Similar to how you would use an augmented assignment syntax such as <code>+=</code> or <code>*=</code> to update a variable, you can do the same for sets:</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>a.update(b)          <span class="hljs-comment"># a "union" operation</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>a &amp;= b               <span class="hljs-comment"># the "intersection" operation</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>a -= set((<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>))  <span class="hljs-comment"># the "difference" operation</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>a ^= b               <span class="hljs-comment"># the "symmetric difference" operation</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>}
</code></pre>
<h1 id="heading-other-set-operations-in-python">Other Set Operations in Python</h1>
<p>These are not so common, but they're useful in seeing how sets relate to others.</p>
<ul>
<li><p>the <code>a.issubset(b)</code> method or <code>&lt;=</code> operator returns true if the <code>a</code> is a <em>subset</em> of <code>b</code></p>
</li>
<li><p>the <code>a.issuperset(b)</code> method or <code>&gt;=</code> operator returns true if the <code>a</code> is a <em>superset</em> of <code>b</code></p>
</li>
<li><p>the <code>a.isdisjoint(b)</code> method return true if there are <em>no common elements</em> between sets <code>a</code> and <code>b</code></p>
</li>
</ul>
<h1 id="heading-frozen-sets-in-python">Frozen Sets in Python</h1>
<p>Because sets are mutable, they are unhashable – which means you cannot use them as dictionary keys.</p>
<p>Python allows you to work around this by using a <code>frozenset</code> instead. This has all the properties of a set, except that it is <em>immutable</em> (this means that you cannot add/remove elements from the frozenset). It is also hashable, so it can be used as keys to a dictionary.</p>
<p>The <code>frozenset</code> datatype has all the methods of a set (such as <code>difference()</code>, <code>symmetric_difference</code>, and <code>union</code>) but because it is immutable, it doesn't have methods to add/remove elements.</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>a = frozenset((<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>b = frozenset((<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>))
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>a.issubset(b)
<span class="hljs-literal">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a.update(b)    <span class="hljs-comment"># raises an error</span>
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
AttributeError: <span class="hljs-string">'frozenset'</span> object has no attribute <span class="hljs-string">'update'</span>
</code></pre>
<p>And using <code>frozenset</code>s as dictionary keys is as simple as 1, 2, 3:</p>
<pre><code class="lang-py"><span class="hljs-meta">&gt;&gt;&gt; </span>a = frozenset((<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>b = frozenset((<span class="hljs-string">"w"</span>, <span class="hljs-string">"x"</span>, <span class="hljs-string">"y"</span>, <span class="hljs-string">"z"</span>))
&gt;&gt;&gt;
<span class="hljs-meta">&gt;&gt;&gt; </span>d = {a: <span class="hljs-string">"hello"</span>, b: <span class="hljs-string">"world"</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>d
{frozenset({<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}): <span class="hljs-string">'hello'</span>, frozenset({<span class="hljs-string">'w'</span>, <span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>, <span class="hljs-string">'z'</span>}): <span class="hljs-string">'world'</span>}
</code></pre>
<h1 id="heading-wrapping-up">Wrapping Up</h1>
<p>That's it! You've learned about what sets are, how to create and work with them, and different operations you can use on them.</p>
<p>With sets done, you should now be comfortable with most of Python built-in functions. All you need to do now is practice. Good luck!</p>
<p>Be sure to <a target="_blank" href="http://twitter.com/jasmcaus">follow me on Twitter</a> for more updates. Have a nice one!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Python Sets: A Detailed Visual Introduction ]]>
                </title>
                <description>
                    <![CDATA[ Welcome In this article, you will learn the fundamentals of Sets in Python. This is a very powerful built-in data type that you can use in your Python projects.  We will explore: What sets are and why they are relevant for your projects. How to crea... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-sets-detailed-visual-introduction/</link>
                <guid isPermaLink="false">66b1f87abafebbb4a1f9f6d2</guid>
                
                    <category>
                        <![CDATA[ Computer Science ]]>
                    </category>
                
                    <category>
                        <![CDATA[ learning to code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ programing ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Sets ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Estefania Cassingena Navone ]]>
                </dc:creator>
                <pubDate>Mon, 06 Jan 2020 13:19:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/12/Sets-3.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <h2 id="heading-welcome">Welcome</h2>
<p>In this article, you will learn the fundamentals of Sets in Python. This is a very powerful built-in data type that you can use in your Python projects. </p>
<p><strong>We will explore:</strong></p>
<ul>
<li>What sets are and why they are relevant for your projects.</li>
<li>How to create a set.</li>
<li>How to check if an element is in a set.</li>
<li>The difference between sets and frozensets.</li>
<li>How to operate with sets (in this part we will dive into the basics of set theory).</li>
<li>How to add and remove elements from sets and how to clear them.</li>
</ul>
<p><strong>Let's begin! ⭐️</strong></p>
<h2 id="heading-sets-in-context">🔹 Sets in Context</h2>
<p>Let me start by telling you why would you want to use sets in your projects. In mathematics, a set is a collection of distinct objects. In Python, what makes them so special is the fact that <strong>they have no duplicate elements</strong>, so they can be used to remove duplicate elements from lists and tuples efficiently. </p>
<p>According to the <a target="_blank" href="https://docs.python.org/3/tutorial/datastructures.html#sets">Python Documentation</a>:</p>
<blockquote>
<p>Python also includes a data type for <em>sets</em>. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries.</p>
</blockquote>
<p><strong>❗️Important:</strong> The elements of a set must be immutable (they cannot be changed). Immutable data types include strings, tuples, and numbers such as integers and floats.</p>
<h2 id="heading-syntax">🔸 Syntax</h2>
<p>To create a set, we start by writing a pair of curly brackets <code>{}</code> and within those curly brackets, we include the elements of the set separated by a comma and a space.  </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-66.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><strong>💡 Tip:</strong> Notice that this syntax is different from Python dictionaries because we are not creating key-value pairs, we are simply including individual elements within curly brackets <code>{}</code>.</p>
<h3 id="heading-set">Set()</h3>
<p>Alternatively, we can use the <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#set">set()</a> function to create a set (see below). </p>
<p>To do this, we would pass an iterable (for example, a list, string, or tuple) and this iterable would be converted to a set, removing any duplicate elements. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-64.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This is an example in IDLE:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Set</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}

<span class="hljs-comment"># From a list</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>set([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>])
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}

<span class="hljs-comment"># From a tuple</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>set((<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>))
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
</code></pre>
<p><strong>💡 Tip:</strong> To create an empty set, you must use the <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#set">set()</a> function because using an empty set of curly brackets, like this <code>{}</code>, will automatically create an empty <strong>dictionary</strong>, not an empty set.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Creates a dictionary, not a set.</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>type({})
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">dict</span>'&gt;

# <span class="hljs-title">This</span> <span class="hljs-title">is</span> <span class="hljs-title">a</span> <span class="hljs-title">set</span>
&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">set(<span class="hljs-params"></span>)</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">set</span>'&gt;</span>
</code></pre>
<h2 id="heading-duplicate-elements-are-removed">🔹 Duplicate Elements are Removed</h2>
<p>If the iterable that you pass as the argument to <code>set()</code> has duplicate elements, they are removed to create the set.</p>
<p>For example, notice how duplicate elements are removed when we pass this list:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>set(a)
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
</code></pre>
<p>and notice how duplicate characters are removed when we pass this string:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-string">"hhheeelllooo"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>set(a)
{<span class="hljs-string">'e'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'h'</span>}
</code></pre>
<h2 id="heading-length">🔸 Length</h2>
<p>To find the length of a set, you can use the built-in function <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#set">len()</a>:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = set(a)
<span class="hljs-meta">&gt;&gt;&gt; </span>len(b)
<span class="hljs-number">4</span>
</code></pre>
<p>In mathematics, the number of elements of a set is called the "<strong>cardinality</strong>" of the set.</p>
<h2 id="heading-membership-testing">🔹 Membership Testing</h2>
<p>You can test if an element is in a set with the <code>in</code> operator:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-65.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This in an example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-string">"hhheeelllooo"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = set(a)
<span class="hljs-meta">&gt;&gt;&gt; </span>b
{<span class="hljs-string">'e'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'h'</span>}

<span class="hljs-comment"># Test if the characters 'e' and 'a' are in set b</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">'e'</span> <span class="hljs-keyword">in</span> b
<span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">'a'</span> <span class="hljs-keyword">in</span> b
<span class="hljs-literal">False</span>
</code></pre>
<h2 id="heading-sets-vs-frozensets">🔸 Sets vs. Frozensets</h2>
<p>Sets are mutable, which means that they can be modified after they have been defined. </p>
<p>According to the <a target="_blank" href="https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset">Python Documentation</a>:</p>
<blockquote>
<p>The <a target="_blank" href="https://docs.python.org/3.8/library/stdtypes.html#set"><code>set</code></a> type is <strong>mutable</strong> — the contents can be changed using methods like <code>add()</code> and <code>remove()</code>. Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.</p>
</blockquote>
<p>Since they cannot contain values of mutable data types, if we try to create a set that contains sets as elements (nested sets), we will see this error:</p>
<pre><code class="lang-python">TypeError: unhashable type: <span class="hljs-string">'set'</span>
</code></pre>
<p>This is an example in IDLE. Notice how the elements that we are trying to include are sets:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>}}
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;pyshell#23&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
    a = {{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>}}
TypeError: unhashable type: <span class="hljs-string">'set'</span>
</code></pre>
<h3 id="heading-frozensets">Frozensets</h3>
<p>To solve this problem, we have another type of set called frozensets. </p>
<p>They are <strong>immutable</strong>, so they cannot be changed and we can use them to create nested sets.</p>
<p>According to the <a target="_blank" href="https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset">Python Documentation</a>:</p>
<blockquote>
<p>The <a target="_blank" href="https://docs.python.org/3.8/library/stdtypes.html#frozenset"><code>frozenset</code></a> type is immutable and <a target="_blank" href="https://docs.python.org/3.8/glossary.html#term-hashable">hashable</a> — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.</p>
</blockquote>
<p>To create a frozenset, we use:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-67.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>💡 <strong>Tip:</strong> You can create an empty frozenset with <code>frozenset()</code>.</p>
<p>This is an example of a set that contains two frozensets:</p>
<pre><code>&gt;&gt;&gt; a = {frozenset([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]), frozenset([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>])}
&gt;&gt;&gt; a
{frozenset({<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}), frozenset({<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>})}
</code></pre><p>Notice that we don't get any errors and the set is created successfully. </p>
<h2 id="heading-introduction-to-set-theory">🔹 Introduction to Set Theory</h2>
<p>Before diving into set operations, we need to explore a little bit of set theory and Venn diagrams. We will dive into each set operation with its corresponding equivalent in Python code. Let's begin. </p>
<h3 id="heading-subsets-and-supersets">Subsets and Supersets</h3>
<p>You can think of a subset as a "smaller portion" of a set. That is how I like to think about it. If you take some of the elements of a set and make a new set with those elements, the new set is a subset of the original set. </p>
<p>It's as if you had a bag full of rubber balls of different colors. If you make a set with all the rubber balls in the bag, and then take some of those rubber balls and make a new set with them, the new set is a subset of the original set. </p>
<p>Let me illustrate this graphically. If we have a set A with the elements 1, 2, 3, 4:</p>
<pre><code>&gt;&gt;&gt; a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
</code></pre><p>We can "take" or "select" some elements of a and make a new set called B. Let's say that we chose to include the elements 1 and 2 in set B:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}
</code></pre>
<p>Every element of B is in A. Therefore, B is a subset of A. </p>
<p>This can be represented graphically like this, where the new set B is illustrated in yellow:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-69.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><strong>💡 Note:</strong> In set theory, it is a convention to use uppercase letters to denote sets. This is why I will use them to refer to the sets (A and B), but I will use lowercase letter in Python (a and b).</p>
<h3 id="heading-issubset">.issubset()</h3>
<p>We can check if B is a subset of A with the method <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#frozenset.issubset">.issubset()</a>:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b.issubset(a)
<span class="hljs-literal">True</span>
</code></pre>
<p>As you can see, B is a subset of A because the value returned is <code>True</code>.</p>
<p>But the opposite is not true since not all the element of A are in B:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a.issubset(b)
<span class="hljs-literal">False</span>
</code></pre>
<p>Let's see something very interesting:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.issubset(b)
<span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b.issubset(a)
<span class="hljs-literal">True</span>
</code></pre>
<p>If two sets are equal, one is a subset of the other and vice versa because all the elements of A are in B and all elements of B are in A. This can be illustrated like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-70.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-using-lt">Using &lt;=</h3>
<p>We can achieve the same functionality of the <code>.issubset()</code> method with the <code>&lt;=</code> comparison operator:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a &lt;= b
<span class="hljs-literal">True</span>
</code></pre>
<p>This operator returns <code>True</code> if the left operand is a subset of the right operand, even when the two sets are equal (when they have the same elements).</p>
<h3 id="heading-proper-subset">Proper Subset</h3>
<p>But what happens if we want to check if a set is a <strong>proper subset</strong> of another? A proper subset is a subset that is not equal to the set (does not have all the same elements). </p>
<p>This would be a graphical example of a proper subset. B does not have all the elements of A:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-69.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>To check this, we can use the <code>&lt;</code> comparison operator:</p>
<pre><code class="lang-python"><span class="hljs-comment"># B is not a proper subset of A because B is equal to A</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b &lt; a
<span class="hljs-literal">False</span>

<span class="hljs-comment"># B is a proper subset of A because B is not equal to A</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b &lt; a
<span class="hljs-literal">True</span>
</code></pre>
<h3 id="heading-superset">Superset</h3>
<p><strong>If B is a subset of A, then A is a superset of B</strong>. A superset is the set that contains all the elements of the subset.  </p>
<p>This can be illustrated like this (see below), where A is a superset of B:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-97.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-issuperset">.issuperset()</h3>
<p>We can test if a set is a superset of another with the <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#frozenset.issuperset">.issuperset()</a> method:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.issuperset(b)
<span class="hljs-literal">True</span>
</code></pre>
<p>We can also use the operators <code>&gt;</code> and <code>&gt;=</code>. They work exactly like <code>&lt;</code> and <code>&lt;=</code>, but now they determine if the left operand is a <strong>superset</strong> of the right operand:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a &gt; b
<span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a &gt;= b
<span class="hljs-literal">True</span>
</code></pre>
<h3 id="heading-disjoint-sets">Disjoint Sets</h3>
<p>Two sets are disjoint if they have no elements in common. For example, here we have two disjoint sets:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-83.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-isdisjoint">.isdisjoint()</h3>
<p>We can check if two sets are disjoint with the <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#frozenset.isdisjoint">.isdisjoint()</a> method:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Elements in common: 3, 1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.isdisjoint(b)
<span class="hljs-literal">False</span>

<span class="hljs-comment"># Elements in common: None</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.isdisjoint(b)
<span class="hljs-literal">True</span>
</code></pre>
<h2 id="heading-set-operations">🔸 Set Operations</h2>
<p>We can operate on sets to create new sets, following the rules of set theory. Let's explore these operations.</p>
<h3 id="heading-union">Union</h3>
<p>This is the first operation that we will analyze. It creates a new set that contains all the elements of the two sets (without repetition).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-72.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This is an example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">7</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a | b
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>}
</code></pre>
<p>💡 <strong>Tip:</strong> We can assign this new set to a variable, like this:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">7</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>c = a | b
<span class="hljs-meta">&gt;&gt;&gt; </span>c
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>}
</code></pre>
<p>In a diagram, these sets could be represented like this (see below). This is called a Venn diagram, and it is used to illustrate the relationships between sets and the result of set operations.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-74.png" alt="Image" width="600" height="400" loading="lazy">
<em>Venn Diagram. Union.</em></p>
<p>We can easily extend this operation to work with more than two sets:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">7</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>c = {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>d = {<span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>}

<span class="hljs-comment"># Union of these four sets</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a | b | c | d
{<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>}
</code></pre>
<p>💡 <strong>Tip:</strong> If the union contains repeated elements, only one is included in the final set to eliminate repetition.</p>
<h3 id="heading-intersection">Intersection</h3>
<p>The intersection between two sets creates another set that contains all the elements that are <strong>in</strong> <strong>both A and B</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-77.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This is an example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a &amp; b
{<span class="hljs-number">1</span>, <span class="hljs-number">3</span>}
</code></pre>
<p>The Venn diagram for the intersection operation would be like this (see below), because only the elements that are <strong>in both A and B</strong> are included in the resulting set:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-78.png" alt="Image" width="600" height="400" loading="lazy">
<em>Venn Diagram. Intersection.</em></p>
<p>We can easily extend this operation to work with more than two sets:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">7</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>c = {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>d = {<span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>}

<span class="hljs-comment"># Only 5 is in a, b, c, and d.</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a &amp; b &amp; c &amp; d
{<span class="hljs-number">5</span>}
</code></pre>
<h3 id="heading-difference">Difference</h3>
<p>The difference between set A and set B is another set that contains all the <strong>elements of set A that are not in set B</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-79.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This is an example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a - b
{<span class="hljs-number">6</span>}
</code></pre>
<p>The Venn diagram for this difference would be like this (see below), because only the elements of A that are not in B are included in the resulting set:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-80.png" alt="Image" width="600" height="400" loading="lazy">
<em>Venn Diagram. Difference.</em></p>
<p>💡 <strong>Tip:</strong> Notice how we remove the elements of A that are also in B (in the intersection). </p>
<p>We can easily extend this to work with more than two sets:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">7</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>c = {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>}

<span class="hljs-comment"># Only 7 is in A but not in B and not in C</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a - b - c
{<span class="hljs-number">7</span>}
</code></pre>
<h3 id="heading-symmetric-difference">Symmetric Difference</h3>
<p>The symmetric difference between two sets A and B is another set that contains <strong>all the elements that are in either A or B, but not both</strong>. We basically remove the elements from the intersection.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-81.png" alt="Image" width="600" height="400" loading="lazy"></p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a ^ b
{<span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>}
</code></pre>
<p>The Venn diagram for the symmetric difference would be like this (see below), because only the elements that are in either A or B, but not both, are included in the resulting set:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-82.png" alt="Image" width="600" height="400" loading="lazy">
<em>Venn Diagram. Symmetric Difference</em></p>
<p>We can easily extend this to work with more than two sets:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">7</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>c = {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>d = {<span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>}

<span class="hljs-meta">&gt;&gt;&gt; </span>a ^ b ^ c ^ d
{<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">7</span>}
</code></pre>
<h3 id="heading-update-sets-automatically">Update Sets Automatically</h3>
<p>If you want to update set A immediately after performing these operations, you can simply add an equal sign after the operator. For example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>b = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}

<span class="hljs-comment"># Notice the &amp;= </span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a &amp;= b
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}
</code></pre>
<p>We are assigning the set that results from <code>a &amp; b</code> to set <code>a</code> in just one line. You can do the same with the other operators: <code>^=</code> , <code>|=</code>, and <code>-=</code>. </p>
<p><strong>💡 Tip:</strong> This is very similar to the syntax that we use with variables (for example: <code>a += 5</code>) but now we are working with sets.</p>
<h2 id="heading-set-methods">🔹 Set Methods</h2>
<p>Sets include helpful built-in methods to help us perform common and essential functionality such as adding elements, deleting elements, and clearing the set.</p>
<h3 id="heading-add-elements">Add Elements</h3>
<p>To add elements to a set, we use the <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#frozenset.add">.add()</a> method, passing the element as the only argument.</p>
<pre><code>&gt;&gt;&gt; a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
&gt;&gt;&gt; a.add(<span class="hljs-number">7</span>)
&gt;&gt;&gt; a
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">7</span>}
</code></pre><h3 id="heading-delete-elements">Delete Elements</h3>
<p>There are three ways to delete an element from a set: <code>.remove(&lt;elem&gt;)</code> ,<code>.discard(&lt;elem&gt;)</code>, and <code>.pop()</code>. They have key differences that we will explore.</p>
<p>The first two methods (.remove() and .discard()) work exactly the same when the element is in the set. The new set is returned:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.remove(<span class="hljs-number">3</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>}

<span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.discard(<span class="hljs-number">3</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>}
</code></pre>
<p>The key difference between these two methods is that if we use the <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#frozenset.remove">.remove()</a> method, we run the risk of trying to remove an element that doesn't exist in the set and this will raise a <code>KeyError</code>:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.remove(<span class="hljs-number">5</span>)
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;pyshell#102&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
    a.remove(<span class="hljs-number">5</span>)
KeyError: <span class="hljs-number">5</span>
</code></pre>
<p>We will never have that problem with <a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#frozenset.discard">.discard()</a> since it doesn't raise an exception if the element is not found. This method will simply leave the set intact, as you can see in this example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.discard(<span class="hljs-number">5</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
</code></pre>
<p>The third method (<a target="_blank" href="https://docs.python.org/3/library/stdtypes.html#frozenset.pop">.pop()</a>) will remove and return an arbitrary element from the set and it will raise a <code>KeyError</code> if the set is empty. </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.pop()
<span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a.pop()
<span class="hljs-number">2</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a.pop()
<span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a
{<span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.pop()
<span class="hljs-number">4</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a
set()
<span class="hljs-meta">&gt;&gt;&gt; </span>a.pop()
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;pyshell#119&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
    a.pop()
KeyError: <span class="hljs-string">'pop from an empty set'</span>
</code></pre>
<h3 id="heading-clear-the-set">Clear the Set</h3>
<p>You can use the <code>.clear()</code> method if you need to delete all the elements from a set. For example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>a.clear()
<span class="hljs-meta">&gt;&gt;&gt; </span>a
set()
<span class="hljs-meta">&gt;&gt;&gt; </span>len(a)
<span class="hljs-number">0</span>
</code></pre>
<h2 id="heading-in-summary">🔸 In Summary</h2>
<ul>
<li>Sets are unordered built-in data types that don't have any repeated elements, so they allow us to eliminate repeated elements from lists and tuples.</li>
<li>They are mutable and they can only contain immutable elements.</li>
<li>We can check if a set is a subset or superset of another set.</li>
<li>Frozenset is an immutable type of set that allows us to create nested sets.</li>
<li>We can operate on sets with: union (<code>|</code>), intersection (<code>&amp;</code>), difference (<code>-</code>), and symmetric difference (<code>^</code>).</li>
<li>We can add elements to a set, delete them, and clear the set completely using built-in methods.</li>
</ul>
<p><strong>I really hope you liked my article and found it helpful.</strong> Now you can work with sets in your Python projects. <a target="_blank" href="https://www.udemy.com/user/estefania-cn/">Check out my online courses</a>. Follow me on <a target="_blank" href="https://twitter.com/EstefaniaCassN">Twitter</a>. ⭐️</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
