Set methods in Python
Tags: python, programming, programmers
Published: 2024-04-07 12:59:04

<h1>&nbsp;</h1>

<p>A&nbsp; <code>set</code>&nbsp;is an abstract data type that represents an&nbsp;unordered collection of unique elements.</p>

<p>Sets just like lists are mutable meaning that operations such as insertion and deletion can be performed on them in-place and without&nbsp;&nbsp;creating a new set.&nbsp;</p>

<p>In Python, sets are represented using the&nbsp;&nbsp;<code>set </code>type/class. This class defines a number of methods to support various&nbsp;operations on sets. The operations can be sub-divided into:&nbsp;</p>

<div>
<ol>
	<li><strong>Elementary operations</strong> such as insertion, deletion, etc.</li>
	<li><strong>Set operations&nbsp;</strong>such as union, intersection, difference, etc .</li>
</ol>
</div>

<h2>Methods for Elementary operations</h2>

<p>Set, like most data structures, supports basic operations such as&nbsp;Insertion and deletion. This is achieved through several&nbsp;methods defined in the <code>set </code>class.</p>

<h4><code>add()</code></h4>

<p>The <code>add() </code>method&nbsp;is used to add a single element to the set. It takes one parameter, which is the element to be added</p>

<div class="syntax">
<pre>
<code class="language-python">myset.add(x)</code></pre>
</div>

<div class="parameters">
<table border="1" cellpadding="1" cellspacing="1" class="table table-bordered table-light" style="width:100%">
	<tbody>
		<tr>
			<td><code>x</code></td>
			<td>The element to be added to the set.</td>
		</tr>
	</tbody>
</table>
</div>

<p>Sets can only contain unique elements thus the operation will have no effect if the element already exists.</p>

<div class="autorun example">
<pre>
<code class="language-python">myset = {1, 2, 3}

#add some elements
myset.add(4)
myset.add(5)

#this has no effect because the elements exists.
myset.add(2)
myset.add(1)

print(myset)</code></pre>
</div>

<h4><code>clear()&nbsp;</code></h4>

<p>The <code>clear()</code> method takes no argument, it removes all elements present in a set effectively turning it to an&nbsp;empty set.</p>

<div class="syntax">
<pre>
<code class="language-python">myset.clear()</code></pre>
</div>

<div class="autorun example">
<pre>
<code class="language-python">#a non-empty set
myset = {1, 2, 3, 4}

#remove all elements
myset.clear()
print(myset)</code></pre>
</div>

<h4><code>copy()</code></h4>

<p>The<code> copy()</code> method is used to&nbsp;create a shallow copy of the set.This means that&nbsp; the method returns a new set object that contains all the elements of the original set but will not be affected if any changes are made to the original set.</p>

<p>The method takes does not take any arguments.</p>

<div class="syntax">
<pre>
<code class="language-python">myset.copy()</code></pre>
</div>

<p>The method returns a new set which is a shallow copy of the original set.</p>

<div class="autorun example">
<pre>
<code class="language-python">myset = {3, 5, 7}

copied = myset.copy()
print(copied)</code></pre>
</div>

<h4><code>discard()</code></h4>

<p>The<code> discard()</code> method removes an element from the <code>set</code> if&nbsp; it is present. It does not raise an exception if&nbsp; the element is not present.</p>

<div class="syntax">
<pre>
<code class="language-python">myset.discard(x)</code></pre>
</div>

<div class="parameters">
<table border="1" cellpadding="1" cellspacing="1" class="table table-bordered table-light" style="width:100%">
	<tbody>
		<tr>
			<td><code>x</code></td>
			<td>The element to be removed.</td>
		</tr>
	</tbody>
</table>
</div>

<div class="autorun example">
<pre>
<code class="language-python">myset = {1, 3, 5, 6, 7, 8, 9}

#remove an element
myset.discard(6)
myset.discard(8)

print(myset)

#no exception is raised if the element does not exist
myset.discard(10)</code></pre>
</div>

<h4><code>pop()&nbsp;</code></h4>

<p>The <code>pop() </code>method removes and returns an arbitrary/random element from the set. It raises a <code>KeyError </code>if the <code>set </code>is empty.</p>

<div class="syntax">
<pre>
<code class="language-python">myset.pop()</code></pre>
</div>

<p>The method takes no arguments, it returns the removed arbitrary element.</p>

<div class="autorun example">
<pre>
<code class="language-python">myset = {0, 2, 4, 6, 8, 10}

#remove and return an arbitrary element
print(myset.pop())
print(myset.pop())
print(myset.pop())
print(myset.pop())

print(myset)</code></pre>
</div>

<h4><code>remove()&nbsp;</code></h4>

<p>The <code>remove()</code> method just like <code>discard()&nbsp;</code>,&nbsp;removes an element from a set if it is a member. However, if the element is not present, <code>remove()</code> will raise a <code>KeyError </code>whereas <code>discard() </code>will not.</p>

<div class="syntax">
<pre>
<code class="language-python">myset.remove(x)</code></pre>
</div>

<div class="parameters">
<table border="1" cellpadding="1" cellspacing="1" class="table table-bordered table-light" style="width:100%">
	<tbody>
		<tr>
			<td><code>x</code></td>
			<td>The element to be removed.</td>
		</tr>
	</tbody>
</table>
</div>

<p>The method removes element <code>x </code>from the <code>set </code>, it raises a <code>KeyError</code>&nbsp;exception if the element is not present.</p>

<div class="autorun example">
<pre>
<code class="language-python">myset = {1, 3, 5, 6, 7, 8, 9}

myset.remove(6)
myset.remove(8)
print(myset)

#raises an exception
myset.remove(10)</code></pre>
</div>

<h4><code>update()&nbsp;</code></h4>

<p>The<code> update()</code>&nbsp; method&nbsp;&nbsp;is used to extend a set&nbsp; with elements from an iterable object.&nbsp;This means elements from the iterable that&nbsp;&nbsp;are not already present in the set will be added to the set.</p>

<div class="syntax">
<pre>
<code class="language-python">myset.extend(iterable)</code></pre>
</div>

<div class="parameters">
<table border="1" cellpadding="1" cellspacing="1" class="table table-bordered table-light" style="width:100%">
	<tbody>
		<tr>
			<td><code>iterable</code></td>
			<td>An iterable object containing the elements to be added to the set.</td>
		</tr>
	</tbody>
</table>
</div>

<div class="autorun example">
<pre>
<code class="language-python">myset = {1, 3, 5}

#The iterable with the elements
elements = [5, 7, 9, 11]

#add elements to the set
myset.update(elements)
print(myset)</code></pre>
</div>

<h2>Methods for set operations&nbsp;</h2>

<p>Apart from the usual operations, sets also supports operations that are only unique to them. Such operations includes Union, intersection,&nbsp; Difference, etc.</p>

<p>In this part we will explore the various methods that are meant for performing set operations.</p>

<h4><code>difference()</code></h4>

<p>The<code> difference()</code> method&nbsp;returns the difference of two or more sets as a new set. &nbsp;The returned set contains only those items from the first set that are not present in the other sets.</p>

<div class="syntax">
<pre>
<code class="language-python">set1.difference(set2 [,set3] [, set4], ...)</code></pre>
</div>

<p>The method takes in arbitrary number of sets as arguments and returns a <code>set </code>with the elements of the original set(<code>set1</code>) which are not present in the input sets. Note that if multiple sets are given as inputs,&nbsp;&nbsp;they will&nbsp;be combined into one set before the operation.</p>

<div class="autorun example">
<pre>
<code class="language-python">set1 = {1, 3, 5, 7, 9, 11}
set2 = {3, 9, 13}
set3 = {7, 9, 11}

diff = set1.difference(set2, set3)
print(diff)</code></pre>
</div>

<h4><code>difference_update()</code></h4>

<p>The<code> difference_update() </code>method is similar to the <code>difference() </code>method in that&nbsp; both returns those elements from one set that are not present in the other(s). However, the&nbsp;<code> difference_update() </code>method updates the original set by removing elements found in the second set rather than creating a new set.</p>

<div class="syntax">
<pre>
<code class="language-python">set1.difference_update(set2 [,set3][, set4]...)</code></pre>
</div>

<p>The method removes elements from <code>set1 </code>that are present in the input sets(<code>set2</code>, <code>set3</code>, etc).</p>

<div class="autorun example">
<pre>
<code class="language-python">set1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
set2 = {0, 2, 4}
set3 = {6, 8}

set1.difference_update(set2, set3)
#elements are removed from the original set
print(set1)</code></pre>
</div>

<h4><code>intersection()</code></h4>

<p>The<code> intersection()</code> method returns the intersection of two sets as a new set. This is a set that contains all the elements that are common between&nbsp; two given sets.</p>

<div class="syntax">
<pre>
<code class="language-python">set1.intersection(set2 [,set3] [,set4]...)</code></pre>
</div>

<p>The method returns elements that are common in both the original <code>set </code>and the input <code>set</code>.</p>

<div class="autorun example">
<pre>
<code class="language-python">set1 = {1, 2, 3, 4, 5}
set2 = {1, 3, 5, 7, 9}

result = set1.intersection(set2)
print(result)</code></pre>
</div>

<h4><code>intersection_update()</code></h4>

<p>This method is just like the<code> intersection() </code>method except that it&nbsp;&nbsp;does not create a new set, instead, it&nbsp;updates the&nbsp;original set.&nbsp;&nbsp;</p>

<div class="autorun example">
<pre>
<code class="language-python">s1 = {1,2,3,4,5} 
s2 = {3,4,5,6,7} 
s1.intersection_update(s2)

print(s1)</code></pre>
</div>

<h4><code>symmetric_difference()&nbsp;</code></h4>

<p>The<code> symmetric_difference() </code>method returns a new set with elements that are in either of the sets, but not in both.</p>

<div class="syntax">
<pre>
<code class="language-python">set1.symmetric_difference(set2) </code></pre>
</div>

<div class="autorun example">
<pre>
<code class="language-python">s1 = {'a', 'b', 'c', 5}
s2 = {3, 5, 7, 'c'}

result = s1.symmetric_difference(s2)
print(result)</code></pre>
</div>

<h4><code>symmetric_difference_update()</code></h4>

<p>Just like <code>symmetric_difference() </code>except that it updates the original list rather than creating a new one.</p>

<div class="syntax">
<pre>
<code class="language-python">set1.symmetric_difference_update(set2) </code></pre>
</div>

<div class="autorun example">
<pre>
<code class="language-python">s1 = {'a', 'b', 'c', 5}
s2 = {3, 5, 7, 'c'}

s1.symmetric_difference_update(s2)
print(s1)</code></pre>
</div>

<h4><code>isdisjoint()</code></h4>

<p>The <code>isdisjoint() </code>method in checks whether two sets have a common item or not. If there are common elements in sets, it&nbsp;returns <code>False </code>otherwise it will return <code>True</code>.</p>

<div class="syntax">
<pre>
<code class="language-python">set1.isdisjoint(set2)</code></pre>
</div>

<p>It returns <code>True </code>if there is no any common element between <code>set1 </code>and <code>set2</code>, otherwise <code>False</code>.&nbsp;</p>

<div class="autorun example">
<pre>
<code class="language-python">s1 = {'a', 'b', 'c'}
s2 = {1, 2, 3, 4}
s3 = {0, 2, 4}

print(s1.isdisjoint(s2))
print(s1.isdisjoint(s3))
print(s2.isdisjoint(s3))</code></pre>
</div>

<p><code>issubset()&nbsp;</code></p>

<p>The <code>issubset()</code> method checks if all elements of a set are present in another set (passed as an argument).&nbsp;</p>

<div class="syntax">
<pre>
<code class="language-python">set1.issubset(set2)</code></pre>
</div>

<p>It returns <code>True </code>if all elements in <code>set1 </code>are present in <code>set2</code>, otherwise <code>False</code>.</p>

<div class="autorun example">
<pre>
<code class="language-python">evens = {0, 2, 4, 6, 8}
odds = {1, 3, 5, 7, 9}
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(evens.issubset(digits))
print(odds.issubset(digits))
print(evens.issubset(odds))</code></pre>
</div>

<h4><code>issuperset()</code></h4>

<p>The<code> issuperset()</code> method checks whether the current set is a superset of the input&nbsp;set. That is&nbsp;if all the elements in the input set are contained within the current set.&nbsp;</p>

<div class="syntax">
<pre>
<code class="language-python">set1.issuperset(set2)</code></pre>
</div>

<p>The method returns <code>True </code>if all elements of <code>set2 </code>are in <code>set1</code>, otherwise <code>False</code>.</p>

<div class="autorun example">
<pre>
<code class="language-python">evens = {0, 2, 4, 6, 8}
odds = {1, 3, 5, 7, 9}
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(digits.issuperset(evens))
print(digits.issuperset(odds))
print(evens.issuperset(odds))</code></pre>
</div>
<div class="related"><h3>Related articles</h3><hr><ul><li><p><a class="text-success" href="https://www.pynerds.com/set-add-method-in-python/">set.add() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-clear-method-in-python/">set.clear() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-difference-method-in-python/">set.difference() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-difference-update-method-in-python/">set.difference_update() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-intersection-method-in-python/">set.intersection() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-intersection-update-in-python/">set.intersection_update() in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-discard-method-in-python/">set.discard() method in Python</a><li><p><a class="text-success" href="/set-pop-method-in-python/">set.pop() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-remove-method-in-python/">set.remove() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-isdisjoint-method-in-python/">set.isdisjoint() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/set-issubset-method-in-python/">set.issubset() method in Python</a></ul></div>