{"id":75102,"date":"2020-01-10T12:00:50","date_gmt":"2020-01-10T06:30:50","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75102"},"modified":"2020-01-10T12:00:50","modified_gmt":"2020-01-10T06:30:50","slug":"python-sets","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-sets\/","title":{"rendered":"Python Sets &#8211; The collection of unordered and unindexed Python objects"},"content":{"rendered":"<p>Till now, we saw various data types in Python which include <strong>numbers<\/strong>, <strong>strings<\/strong>, <strong>lists<\/strong>, <strong>tuples<\/strong>, and <strong>dictionaries<\/strong>.<\/p>\n<p>Today, we are going to see another data type that is <strong>Python Sets<\/strong>. We will see what sets are and how you can <strong>create<\/strong>, <strong>access<\/strong> and <strong>perform operations<\/strong> on them. We will also see the functions associated with them.<\/p>\n<p>So let&#8217;s start with the concept of Python sets.<\/p>\n<h3>Python Sets<\/h3>\n<p>Sets in Python are a <strong>collection<\/strong> of <strong>unordered<\/strong> and <strong>unindexed<\/strong> <strong>Python objects<\/strong>. Sets are <strong>mutable<\/strong>, <strong>iterable<\/strong> and they <strong>do not<\/strong> contain <strong>duplicate values<\/strong>. It is similar to the concept of the <strong>mathematical set<\/strong>.<\/p>\n<h3>Create a Set in Python<\/h3>\n<p>The elements in a set are declared inside<strong> curly braces<\/strong> separated by <strong>commas<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#sets are unordered, it rearranges itself in sorted order\na = {1, 2, 7, 5, 5}\nb = {99, 100, 200, \u201cyes\u201d, \u201cno\u201d}\n\n#empty set are created using the set() function\nempty_set = set()\nprint(empty_set)\nprint(type(empty_set))\nempty_dictionary = {}\nprint(type(empty_dictionary))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">set()<br \/>\n&lt;class \u2018set\u2019 &gt;<br \/>\n&lt;class \u2018dict\u2019 &gt;<\/div>\n<h3>Accessing Python Sets<\/h3>\n<p>As we discussed earlier that sets in Python are <strong>unindexed<\/strong>, so we access the entire set at once in <strong>sorted order<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = {7, 6, 5, 4, 3, 4, 5, 6}\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{3, 4, 5, 6, 7}<\/div>\n<p>See how the elements are <strong>sorted<\/strong> and <strong>duplicates<\/strong> are reduced in sets.<\/p>\n<p>Since the sets are <strong>unindexed<\/strong>, we <strong>cannot<\/strong> use <strong>slicing<\/strong> or <strong>indexing<\/strong> on sets, it will give us a <strong>TypeError<\/strong> error when using <strong>indexing<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = {1,2,3,4}\nprint(a[2])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;stdin&gt;&#8221;, line 2, in &lt;module&gt;<br \/>\nTypeError: &#8216;set&#8217; object does not support indexing<\/div>\n<h3>Iterating on Sets in Python<\/h3>\n<p>In python sets, they are <strong>iterable<\/strong> and we can iterate over them with a<strong> \u2018for\u2019 loop<\/strong>. Don\u2019t expect the <strong>order of elements<\/strong> to be the same as the <strong>original<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">ratings = {\u2018A\u2019, \u2018B\u2019, \u2018C\u2019, \u2018D\u2019}\nfor rate in ratings:\n  print(rate)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">B<br \/>\nD<br \/>\nA<br \/>\nC<\/div>\n<h3>Deleting Elements from Python Sets<\/h3>\n<p>In python sets, to delete the element, we usually have to specify the <strong>element<\/strong> we want to delete. The functions used for deleting element are <strong>discard()<\/strong>, <strong>remove()<\/strong>, <strong>pop()<\/strong> and <strong>clear()<\/strong>.<\/p>\n<h4>1. discard(element)<\/h4>\n<p>The function <strong>deletes<\/strong> the <strong>specified element<\/strong> from the set and if the element is <strong>not present<\/strong> in the set, it <strong>does not<\/strong> return any <strong>error<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a={1,2,3,4}\na.remove(3)\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 4}<\/div>\n<h4>2. remove(element)<\/h4>\n<p>This function is similar to <strong>discard()<\/strong> but the difference is that it throws an <strong>error<\/strong> when the specified element to delete is <strong>not found<\/strong> in the set.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a={10,20,30}\na.remove(10)\nprint(10)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{20, 30}<\/div>\n<h4>3. pop()<\/h4>\n<p><strong>pop() function<\/strong> doesn\u2019t take any argument and it <strong>removes<\/strong> an <strong>arbitrary element<\/strong> from the set and also <strong>returns<\/strong> the <strong>deleted element<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">num_set={10,8,6,4}\ndeleted = num_set.pop()\nprint(deleted)\nprint(num_set)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">8<br \/>\n{10,4,6}<\/div>\n<h4>4. clear()<\/h4>\n<p>The clear() function <strong>empties<\/strong> the set. It can also be used for <strong>dictionaries<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = {1,5,10,15,20}\na.clear()\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">set()<\/div>\n<p>As we know, an <strong>empty set<\/strong> is denoted as <strong>set()<\/strong> and<strong> not <\/strong>by<strong> {}<\/strong>.<\/p>\n<h3>Updating a Set in Python<\/h3>\n<p>Set are <strong>mutable<\/strong> so we can <strong>remove elements<\/strong> and also <strong>add<\/strong> or <strong>update new elements<\/strong> to the set. We have <strong>two functions<\/strong> to update the values in the set. These are<strong> add()<\/strong> and <strong>update()<\/strong> methods.<\/p>\n<p>Let\u2019s look at each of them.<\/p>\n<h4>1. add(element)<\/h4>\n<p>The <strong>add()<\/strong> function in python sets takes the value as an <strong>argument<\/strong> which we want to add to our set and <strong>adds<\/strong> the <strong>element in<\/strong> our <strong>set<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">fruits = {\u201capple\u201d, \u201cbanana\u201d,\u201dcherry\u201d}\nfruits.add(\u201cstrawberry\u201d)\nprint(fruits)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{&#8216;apple&#8217;, &#8216;banana&#8217;, &#8216;cherry&#8217;, &#8216;strawberry&#8217;}<\/div>\n<h4>2. update(updated_set)<\/h4>\n<p>The <strong>update()<\/strong> function in python sets can be used to <strong>add multiple values<\/strong> to the set at once. It takes an <strong>iterable<\/strong> like <strong>lists<\/strong>, <strong>set<\/strong> or <strong>tuple<\/strong> as an argument.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">fruits = {\u201capple\u201d, \u201cbanana\u201d,}\nfruits.update([1,2,3])\nprint(fruits)\nfruits.update({\u2018cherry\u2019, \u2019kiwi\u2019 })<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3, &#8216;apple&#8217;, &#8216;banana&#8217;}<br \/>\n{1, 2, 3, &#8216;apple&#8217;, &#8216;banana&#8217;, \u2018kiwi\u2019}<\/div>\n<h3>Frozen Sets in Python<\/h3>\n<p>So far we have seen sets are <strong>mutable<\/strong>, but now I am going to tell you about <strong>immutable<\/strong> sets.<\/p>\n<p>Frozen sets are similar to sets but the only difference is that frozen sets are <strong>immutable<\/strong>.<\/p>\n<p>Let\u2019s see how we can create a frozen set:<\/p>\n<p>The <strong>frozenset()<\/strong> inbuilt function is used to <strong>create<\/strong> a <strong>frozen set<\/strong>. We can pass any iterable like <strong>list<\/strong>, <strong>set<\/strong>, <strong>tuple<\/strong>, etc and it will return us an immutable set of <strong>class type<\/strong> <strong>frozenset<\/strong>.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">frozenset(iterable)<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">immutabl_set = frozenset((2,4,2,1,2,3))\nprint(immutable_set)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">frozenset({1, 2, 3, 4})<\/div>\n<p><strong>Note:<\/strong> Since <strong>frozenset<\/strong> are <strong>immutable<\/strong> sets, we can\u2019t <strong>update<\/strong> or <strong>change<\/strong> any <strong>index values<\/strong>, the interpret will give us an <strong>error<\/strong> if we try to change them.<\/p>\n<h3>Python Set Operations<\/h3>\n<p>In python sets, we can perform various operations on set using functions <strong>len()<\/strong>, <strong>max()<\/strong>, <strong>min()<\/strong>, <strong>any()<\/strong>, <strong>all()<\/strong>, <strong>sorted()<\/strong>.<\/p>\n<p>Let\u2019s look at each of these functions working.<\/p>\n<h4>1. len()<\/h4>\n<p>The len() function is useful to know the <strong>number of elements<\/strong> inside an <strong>iterator<\/strong>. This function will return us the <strong>length<\/strong> of the set.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a={1,2,2,3,3,4}\nprint(len(a))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<h4>2. max() and min()<\/h4>\n<p>Functions like <strong>max()<\/strong> and <strong>min()<\/strong> require the set to contain the <strong>same data type<\/strong> elements to work with because we can\u2019t compare <strong>integers<\/strong> and <strong>strings<\/strong> directly.<\/p>\n<p>The function max() returns the <strong>maximum<\/strong> <strong>value<\/strong> from the set and the min() function returns the <strong>minimum<\/strong> <strong>value<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a={1,2,3,5,2,9,10}\nprint( max(a))\nprint( min(a))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">10<br \/>\n1<\/div>\n<h4>3. any() and all()<\/h4>\n<p>These functions return us <strong>True<\/strong> or <strong>False<\/strong>. The <strong>any() function<\/strong> is used to check if any one of the values is <strong>True<\/strong> or not while the <strong>all() function<\/strong> will return True if all values result to <strong>boolean True<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(any({0, False}) )\nprint(all({1, \u2018Hello\u2019, True}) )<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">False<br \/>\nTrue<\/div>\n<h4>4. sorted()<\/h4>\n<p>The sorted() function takes an <strong>iterable<\/strong> like sets and <strong>sorts<\/strong> the <strong>elements<\/strong> in <strong>ascending order<\/strong>. It <strong>preserves<\/strong> the <strong>original array<\/strong> and <strong>returns<\/strong> a <strong>new array<\/strong> after sorting.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sorted({6,5,4,3,4,5,2,1,4})<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<p>Now, we will see some mathematical functions as we discussed that Python sets are related to <strong>mathematical set concept<\/strong>.<\/p>\n<h4>5. union()<\/h4>\n<p>Let\u2019s take an example of two sets A and B. The union operation between A and B will <strong>return<\/strong> all the <strong>distinctive elements<\/strong> in <strong>A<\/strong> and <strong>B<\/strong>.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-union-operation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75362 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-union-operation.jpg\" alt=\"python sets - union operation\" width=\"518\" height=\"311\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">A= {1, 2, 3, 4} ; B= {2, 4, 6, 4}\nprint( A.union(B) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3, 4, 6}<\/div>\n<h4>6. intersection()<\/h4>\n<p>The intersection operation <strong>returns<\/strong> the set of <strong>elements<\/strong> that are <strong>common<\/strong> in the sets that are <strong>used<\/strong> in <strong>intersection<\/strong>.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-intersection-operation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75363 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-intersection-operation.jpg\" alt=\"python sets - intersection operation\" width=\"518\" height=\"311\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">A= {1, 2, 3, 4} ; B= {2, 4, 6, 4}\nprint( A.intersection(B) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{ 2, 4 }<\/div>\n<p>To operate on more sets, you can pass them with <strong>commas<\/strong> in an argument.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print({1,2,3}.intersection({2,3,4,5}, {1,3,6}) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{3}<\/div>\n<h4>7. difference()<\/h4>\n<p>The <strong>difference()<\/strong> function on sets performs the <strong>set difference operation<\/strong> on <strong>two<\/strong> or <strong>more sets<\/strong>.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-difference-function.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75364 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-difference-function.jpg\" alt=\"difference function in python sets\" width=\"590\" height=\"311\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print({1,2,3,4}.difference({3,4,5,6} ) )\nprint({1,2,3,4}.difference({3,4,5,6},{ 1,3,4,5} ) )<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">{1, 2}<br \/>\n{2}<\/div>\n<h4>8. symmetric_difference()<\/h4>\n<p>The function returns the <strong>unique elements<\/strong> in each set.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-symmetric-difference.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75365 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-symmetric-difference.jpg\" alt=\"symmetric difference in python sets\" width=\"518\" height=\"311\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print({1,2,4,5,6}.symmetric_difference({ 4, 5, 6, 7, 8 }) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 7, 8}<\/div>\n<h4>9. intersection_update(), difference_update() and <strong>symmetric_difference_update()<\/strong><\/h4>\n<p>As we saw earlier that these functions return a new set and <strong>do not change<\/strong> the <strong>original set<\/strong>. So these functions will <strong>perform<\/strong> and <strong>update<\/strong> the <strong>original set<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">set1 = {1,2,3} ; set2 = {2,3,4}\nset1.intersection_update(set2)\nprint(set1)\nset1.difference_update(set2)\nprint(set1)\nset1.symmetric_difference_update(set2)\nprint(set1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{2,3}<br \/>\nset()<br \/>\n{2,3,4}<\/div>\n<h4>10. isdisjoint()<\/h4>\n<p>The <strong>isdisjoint()<\/strong> function takes only <strong>one argument<\/strong> and <strong>checks<\/strong> whether the <strong>two sets<\/strong> are completely <strong>different<\/strong> or not. In other words, their <strong>intersection<\/strong> should be <strong>null<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( {1,2,3}.isdisjoint( {5,6,7} ) )\nprint( {1,2,3}.isdisjoint( {5,2,7} ) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<br \/>\nFalse<\/div>\n<h4>11. issubset()<\/h4>\n<p>The<strong> issubset() function<\/strong> returns <strong>True<\/strong> if the set is a proper <strong>subset<\/strong> of the set given in argument.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( {1,2,3}.issubset( {1,2,3,5,6,} ) )\nprint( {1,2,8}.issubset( {1,2,3,5,6,} ) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<br \/>\nFalse<\/div>\n<h4>12. issuperset()<\/h4>\n<p>Like the<strong> issubset()<\/strong> method, this function returns <strong>True<\/strong> when the set is a <strong>superset<\/strong> of the set given in argument.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( {1,2,3}.issuperset( {1, 2} ) )\nprint( {1,2,3}.issuperset( {1, 4} ) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<br \/>\nFalse<\/div>\n<h4>13. Membership<\/h4>\n<p>In python sets, the python operators <strong>\u2018in\u2019<\/strong> and <strong>\u2018not in\u2019<\/strong> are called <strong>membership operators<\/strong> that checks if an element is <strong>present<\/strong> in the <strong>collections of elements<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( \u201cCherry\u201d in {\u201cApple\u201d, \u201cCherry\u201d, \u201cKiwi\u201d} )\nprint( \u201cCherry\u201d not in {\u201cApple\u201d, \u201cCherry\u201d, \u201cKiwi\u201d} )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<br \/>\nFalse<\/div>\n<h3>Summary<\/h3>\n<p>To sum up, we first saw Python sets which holds unique values and then saw how to <strong>access<\/strong> them, <strong>delete<\/strong> and <strong>update<\/strong> the <strong>elements in sets<\/strong>. Later on, we discussed all the <strong>operations<\/strong> you can perform on Python sets.<\/p>\n<p>So that was all about TechVidvan&#8217;s Python sets article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Till now, we saw various data types in Python which include numbers, strings, lists, tuples, and dictionaries. Today, we are going to see another data type that is Python Sets. We will see what&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75366,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1275,1276,1277,1278,1279,1280,1281,1282,1283,1284],"class_list":["post-75102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-accessing-python-sets","tag-create-a-set-in-python","tag-deleting-elements-from-python-sets","tag-iterating-on-sets-in-python","tag-python-frozen-sets","tag-python-membership-operators","tag-python-set-operations","tag-python-sets","tag-sets-in-python","tag-updating-a-set-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Sets - The collection of unordered and unindexed Python objects - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python sets - Explore sets in Python the important data type and learn to create, access &amp; perform operations on them and its essential functions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-sets\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Sets - The collection of unordered and unindexed Python objects - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python sets - Explore sets in Python the important data type and learn to create, access &amp; perform operations on them and its essential functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-sets\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-10T06:30:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-sets.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Sets - The collection of unordered and unindexed Python objects - TechVidvan","description":"Python sets - Explore sets in Python the important data type and learn to create, access & perform operations on them and its essential functions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-sets\/","og_locale":"en_US","og_type":"article","og_title":"Python Sets - The collection of unordered and unindexed Python objects - TechVidvan","og_description":"Python sets - Explore sets in Python the important data type and learn to create, access & perform operations on them and its essential functions.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-sets\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-10T06:30:50+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-sets.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Sets &#8211; The collection of unordered and unindexed Python objects","datePublished":"2020-01-10T06:30:50+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/"},"wordCount":1152,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-sets.jpg","keywords":["Accessing Python Sets","Create a Set in Python","Deleting Elements from Python Sets","Iterating on Sets in Python","python frozen sets","python membership operators","Python Set Operations","Python Sets","sets in python","Updating a Set in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-sets\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/","url":"https:\/\/techvidvan.com\/tutorials\/python-sets\/","name":"Python Sets - The collection of unordered and unindexed Python objects - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-sets.jpg","datePublished":"2020-01-10T06:30:50+00:00","description":"Python sets - Explore sets in Python the important data type and learn to create, access & perform operations on them and its essential functions.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-sets\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-sets.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-sets.jpg","width":802,"height":420,"caption":"sets in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-sets\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Sets &#8211; The collection of unordered and unindexed Python objects"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75102"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75102\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75366"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}