iv id="f0c3"><pre>pop() <span class="hljs-keyword">method</span> <span class="hljs-title function_">will</span> <span class="hljs-title function_">return</span> <span class="hljs-title function_">the</span> <span class="hljs-title function_">removed</span> <span class="hljs-title function_">element</span>.<span class="hljs-title function_">Return</span> <span class="hljs-title function_">value</span> <span class="hljs-title function_">is</span> <span class="hljs-title function_">removed</span> <span class="hljs-title function_">element</span>.</pre></div><p id="8e55"><b>copy()</b></p><p id="8df0">copy method will return a copy of the set.</p>
<figure id="a861">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/fd2f9891a4387e1884e435cb2e935ba0.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="36a6">Mathematical Set Operations</h2><ol><li><b>union(): </b>Return new set containing union of both set.Union means return all elements from both sets.Union is performed by <b>|</b>operator or by<b> union() </b>method</li><li><b>intersection()</b>: Return new set containing intersection of both set.Intersection means returns only common element between both set.Intersection is performed by <b>&</b> operator or by <b>intersection()</b> method.</li></ol>
<figure id="0b42">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/26f4fc19600e518473c6d8b87eba26f1.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="a5db">3. <b>difference(): </b>Returns a new set containing the difference between both sets. <b>A-B </b>means returns elements from set A which is not present in set B. The difference can be performed by <code><b>-</b></code>operator or using the <b>difference()</b> method.</p><p id="94bf">4. <b>difference_update(): </b>Removes the element from the set which is also present in the other set.It is performed by using<code> <b>-=</b></code><b> </b>operator or by using the <b>difference_update()</b> method. The return type is None. It will modify the original set itself.</p>
<figure id="3045">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/8d349f5102b3ad2c317f6fac299cb2eb.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="a59d">5. <b>symmetric_difference(): </b>Returns a new set containing elements from both sets excluding common elements in both sets.It is performed by using <code><b>^</b></code> operator or by using the <b>symmetric_difference()</b> method.</p><p id="addf">6. <b>symmetric_difference_update(): </b>Updates the set by keeping elements in both sets excluding common elements in both sets. It is performed by using ^= operator or by using the <b>symmetric_difference_update() </b>method. The return type is None. It will modify the original set itself.</p>
<figure id="225d">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/d37e0dfbf38575cac1980de4bfc4f4ef.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="8ce2">7.<b>intersection_update(): </b>Updates the set with elements that are common to both sets.It is performed by <code><b>&=</b></code> operator or by using intersection_update() method.Return Type is None. It will update the original set itself.</p>
<figure id="f4ca">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/cc782fa30ce1673e3ae3c57786c9fe2e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="435b">8. <b>isdisjoint():</b> Returns <code><b>True</b> </code>if both sets have no common elements.Returns <code><b>False</b></code> if both sets have common elements.</p>
<figure id="39ff">
<div>
<div>
<ifram
Options
e class="gist-iframe" src="/gist/IndhumathyChelliah/9872b8f19722f1f51238526d60bc53e2.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="b427">9. <b>issubset(): </b>Returns <code><b>True</b></code> if all elements in this set are present in the specified set or otherwise Returns <code><b>False</b>.</code></p>
<figure id="29d1">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/0e498fa9126bec1c0718a638f43abafc.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="9bb4">10. <b>issuperset(): </b>Returns<b> True</b> if all elements in the specified set are present in the original set or otherwise Returns <b>False</b>.</p>
<figure id="6ac6">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/ae96f26502217c9ac0e0a28d6baa93ee.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="15e4">Membership Operations</h2><p id="425f">We can check whether an item is present in the set or not by using <code>in</code> keyword.</p>
<figure id="b85e">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/89f12a6b737c9634c7f16fe32ac4e75f.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="4808">Built-in Functions -Set</h2><ol><li><b>len()</b>: Returns the number of elements in the set</li><li><b>min():</b> Returns the smallest element in the set</li><li><b>max():</b> Returns the largest element in the set</li><li><b>sum(): </b>Returns the sum of all elements in the set.</li></ol>
<figure id="a81d">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/15102d3715838a75cd397081bfe81eb5.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="e5ec"><b>5. sorted(): </b>Returns a new <code>list</code> containing sorted elements. Since the set is unordered, the return type is List.</p>
<figure id="e296">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/8bb8891feeda8da5e7302b3ab4cc73a4.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="5ca7"><b>6. all():</b> Returns <code>True</code> if all elements in the set are <code>True </code>or if the set is empty.</p><p id="facc"><b>7. any(): </b>Returns True if any element in the set is <code>True</code>.If the set is empty returns <code>False</code></p>
<figure id="9292">
<div>
<div>
<iframe class="gist-iframe" src="/gist/IndhumathyChelliah/5ff5583b11a74c7ad193440ce2fd8a7a.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="1778">Conclusion:</h2><ul><li>Set methods which will modify the original set.
<b>Return Type is None.</b>
add(),update(),remove(),discard(),clear(),difference_update(), symmetric_difference_update(),intersection_update()</li><li><b>Return Type Boolean</b>
isdisjoint(),issuperset(),issubset(),any(),all()</li><li><b>Return Type List</b>
sorted()</li><li><b>Return type removed element</b>
pop()</li><li><b>Returns new set and won’t modify the original set.</b>
union(),intersection(),difference(),symmetric_difference()</li></ul><h2 id="278c">My other blogs on Python data structures</h2><p id="8ab9"><a href="https://readmedium.com/python-list-79ef2e183d60">An Introduction to Python List</a></p><p id="9882"><a href="https://readmedium.com/an-introduction-to-python-tuple-adc0a0fe024d">An Introduction to Python Tuple</a></p><p id="b632"><a href="https://readmedium.com/an-introduction-to-python-dictionary-520302924ef8">An Introduction to Python Dictionary</a></p></article></body>
Set is a data structure in Python, which is unordered and unindexed. The set has no duplicate elements. Every set element is
unique and immutable but the set itself is mutable. That means we can’t change the value of the set element but we can add or remove elements from the set. Sets are written within curly brackets.
Topics covered in this story
Image by Author
Creating Python Set
Accessing Set Elements
Set elements are unordered, so it can’t be accessed. But we can loop through the set element.
Add Items to Set
We can use add() and update() method to add elements to set. add() used to add one element to set. update() method used to add multiple elements to set.
Note: Both add() and update() methods will modify the original set itself. It won’t return anything. The return type is None.
Removing an Element from the Set
remove()
discard()
pop()
clear()
del
remove()
remove() will remove the specified element from the set. If the element is not in set means, it will raise an error.
discard()
discard() will also remove the specified element from the set. If the element is not in set means, it won’t raise an error.
pop()
Since the set is unordered, pop() will remove any element from the set.
clear()
clear() will empty the set.
del
del keyword will delete the set itself.
Note: remove(),discard(),clear() methods will modify the original set itself.It won’t return anything.ReturnTypeisNone.
union(): Return new set containing union of both set.Union means return all elements from both sets.Union is performed by |operator or by union() method
intersection(): Return new set containing intersection of both set.Intersection means returns only common element between both set.Intersection is performed by & operator or by intersection() method.
3. difference(): Returns a new set containing the difference between both sets. A-B means returns elements from set A which is not present in set B. The difference can be performed by -operator or using the difference() method.
4. difference_update(): Removes the element from the set which is also present in the other set.It is performed by using-=operator or by using the difference_update() method. The return type is None. It will modify the original set itself.
5. symmetric_difference(): Returns a new set containing elements from both sets excluding common elements in both sets.It is performed by using ^ operator or by using the symmetric_difference() method.
6. symmetric_difference_update(): Updates the set by keeping elements in both sets excluding common elements in both sets. It is performed by using ^= operator or by using the symmetric_difference_update() method. The return type is None. It will modify the original set itself.
7.intersection_update(): Updates the set with elements that are common to both sets.It is performed by &= operator or by using intersection_update() method.Return Type is None. It will update the original set itself.
8. isdisjoint(): Returns Trueif both sets have no common elements.Returns False if both sets have common elements.
9. issubset(): Returns True if all elements in this set are present in the specified set or otherwise Returns False.
10. issuperset(): Returns True if all elements in the specified set are present in the original set or otherwise Returns False.
Membership Operations
We can check whether an item is present in the set or not by using in keyword.
Built-in Functions -Set
len(): Returns the number of elements in the set
min(): Returns the smallest element in the set
max(): Returns the largest element in the set
sum(): Returns the sum of all elements in the set.
5. sorted(): Returns a new list containing sorted elements. Since the set is unordered, the return type is List.
6. all(): Returns True if all elements in the set are True or if the set is empty.
7. any(): Returns True if any element in the set is True.If the set is empty returns False
Conclusion:
Set methods which will modify the original set.
Return Type is None.
add(),update(),remove(),discard(),clear(),difference_update(), symmetric_difference_update(),intersection_update()
Return Type Boolean
isdisjoint(),issuperset(),issubset(),any(),all()
Return Type List
sorted()
Return type removed element
pop()
Returns new set and won’t modify the original set.
union(),intersection(),difference(),symmetric_difference()